如何让ajax从php接收多个数据,其中一个数据处于循环中

时间:2014-03-14 07:16:30

标签: php mysql ajax

我知道JSON可以帮助我通过将所有数据存储在数组中来处理来自php的多个数据。但是其中一个数据是循环中的呢?请参阅以下代码:

$data1='<p>hehe</p>';
$data2='<h1>wawa</h1>'; 
$backarr=array($data1,$data2);

echo json_encode($backarr);

现在我有一些想成为data3的东西

$sql="SELECT username FROM users WHERE username != '$username'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
    $names = $row['username'];
    echo '<p>'.$names.'</p>';
} 

有可能吗?我希望ajax能够接收所有三个数据,如何处理data3?

3 个答案:

答案 0 :(得分:2)

真的不是那么难:

$names = array();
while($row = mysql_fetch_array( $result )) 
{
   array_push($names, "<p>" . $row['username'] . "</p>");
}

echo json_encode($names);

答案 1 :(得分:0)

您可以将所有数组合在一起,然后开始编码。

$dataSet = array();
$data1='<p>hehe</p>';
$data2='<h1>wawa</h1>'; 
$dataSet[] = $backarr = array($data1,$data2);


$sql="SELECT username FROM users WHERE username != '$username'"; 
$result = mysql_query($sql) or die(mysql_error()); 
while($row = mysql_fetch_array( $result )) { 
    $names[] = $row['username'];
} 
$dataSet[] = $names;

echo json_encode($dataSet);

修改::

这里是jQuery的东西:

jQuery.post('url/script.php',{fieldName : fieldValue},function(res){
   console.log(res);
   var res = jQuery.parseJSON(res);
   console.log(res);
});

Pure AJAX:

<script type="application/javascript">
function loadJSON()
{
   var data_file = "http://www.example.com/data.json";
   var http_request = new XMLHttpRequest();
   try{
      // Opera 8.0+, Firefox, Chrome, Safari
      http_request = new XMLHttpRequest();
   }catch (e){
      // Internet Explorer Browsers
      try{
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      }catch (e) {
         try{
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         }catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }
   http_request.onreadystatechange  = function(){
      if (http_request.readyState == 4  )
      {
        // Javascript function JSON.parse to parse JSON data
        var jsonObj = JSON.parse(http_request.responseText);

        // jsonObj variable now contains the data structure and can
        // be accessed as jsonObj.name and jsonObj.country.
        document.getElementById("Name").innerHTML =  jsonObj.name;
        document.getElementById("Country").innerHTML = jsonObj.country;
      }
   }
   http_request.open("GET", data_file, true);
   http_request.send();
}
</script>

答案 2 :(得分:0)

试试这个

$backarray = array();
$backarray['data1'] = '<p>hehe</p>';
$backarray['data2'] = '<h1>wawa</h1>';


$sql="SELECT username FROM users WHERE username != '$username'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
    $names[] = $row['username'];
}

//Push $names array into $backarray
$backarray['names'] = $names;

var_dump(json_encode($backarray));

将输出类似于数组中所有名称的内容。

'{"data1":"<p>hehe<\/p>","data2":"<h1>wawa<\/h1>","names":["Name1","Name2","Name3","Name4","Name5","Name6"]}'