从php发送两个警报数据回到我的脚本

时间:2016-09-06 05:42:05

标签: php ajax

我有2个php文件: -

第一个:=

[ {Language=en_US, ArticleNumber=000106422, PublishStatus=Draft}, 
  {Language=en_US, ArticleNumber=000106422, PublishStatus=Online} ]

第二个=

<?php
echo "Connected successfully  ";

echo "here";

?>

问题在于此处只有一个警报值: - function retrieveData(){ $.post('retrieveData.php',{}, function(data){ window.alert (data) }); } 显示。

我需要数据在每次在另一个Connected successfully文件中找到echo时发出警报。

换句话说,我希望我会用php提醒一个人 以及其他Connected successfully

请帮助

2 个答案:

答案 0 :(得分:2)

这是其他变种

第一个文件retrieveData.php

<?php
$array = array("message"=>"Connected successfully","text"=>"heree");
echo json_encode($array);
?>

第二个文件index.php

<script src = "https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script>
$(document).ready(function(){
 retrieveData();

   function retrieveData(){
     $.post('retrieveData.php',{}, function(data){
        data=$.parseJSON(data);
        window.alert (data.message);
        window.alert (data.text);
     });
  }
 });      

答案 1 :(得分:0)

直接使用echo,一次发送两个或多个警报数据很复杂(复杂但可行)。

您可以通过以下简单方式完成: -

第一页(PHP页面):

<?php
$data = array(); // create an array
$data[] =  "Connected successfully  "; // add all echo data to the array

$data[] =  "heree"; // add all echo data to the array

echo json_encode($data); // encode the array into json and send it back to jQuery code as a response
?>

第二页:

<script src = "https://code.jquery.com/jquery-3.1.0.min.js"></script>

<script>
$(document).ready(function(){
    retrieveData();
   function retrieveData(){
        $.post('abc.php',{}, function(data){
            var new_data = $.parseJSON(data); // decode json data and covert it to jQuery array

            $.each(new_data, function(index, element) { // iterate over array
               window.alert(element); // alert each element of the array
            });
        });
    }
});
</script>

我本地的输出:

  1. http://prntscr.com/ceofn6

  2. http://prntscr.com/ceofsd

  3. 正如你在评论中提出的那样,如下所示:

    $data = array();
    if ($result->num_rows > 0) { 
    while( $row = $result->fetch_assoc()) {
         $data[] = $row["input"]; // make sure that db table column name is "input"
    }
    echo json_encode($data); 
    
相关问题