Javascript 将数据从php传回ajax

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10960471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:47:14  来源:igfitidea点击:

Passing data back from php to ajax

phpjavascriptajaxjquery

提问by Yahoo

How can I pass data from a php of then rows back to ajax ?

如何将数据从 php 的行传回 ajax ?

PHP

PHP

$query = 'SELECT * FROM picture order by rand() LIMIT 10';
$result = mysql_query($query);

while ($rec = mysql_fetch_array($result,  MYSQL_ASSOC)) {

$url[]=$rec['pic_location'];
$name[]=$rec['name'];
$age[]=$rec['age'];
$gender[]=$rec['gender'];


}

echo json_encode($url);
echo json_encode($name);
echo json_encode($age);
echo json_encode($gender);

Ajax

阿贾克斯

$(".goButton").click(function() {
   var dir =  $(this).attr("id");
   var imId = $(".theImage").attr("id");

   $.ajax({
      url: "viewnew.php",
      dataType: "json",
      data: {
         current_image: imId,
         direction    : dir
      },
      success: function(ret){
          console.log(ret);
          var arr = ret;
          alert("first image url: " + arr[0][0] + ", second image url: " + arr[0][1]);  // This code isnt working
          alert("first image Name: " + arr[1][0] + ", second image name: " + arr[1][1]);
          $(".theImage").attr("src", arr[0]);
          if ('prev' == dir) {
            imId ++;
         } else {
            imId --;
         }
         $("#theImage").attr("id", imId);
      }
   });

});
});
</script>

My question is how can I display the values here ? The Alert message is giving me "Undefined" ?

我的问题是如何在此处显示值?警报消息给我“未定义”?

回答by Alexander

You can do something along these lines.

你可以沿着这些方向做一些事情。

PHP

PHP

$query = 'SELECT * FROM picture order by rand() LIMIT 10';
$res = mysql_query($query);

$pictures = array();
while ($row = mysql_fetch_array($res)) {
  $picture = array(
    "pic_location" => $row['pic_location'],
    "name"         => $row['name'],
    "age"          => $row['age'],
    "gender"       => $row['gender']
  );
  $pictures[] = $picture;
}

echo json_encode($pictures);

JS

JS

...
$.ajax({
  ...
  dataType: "json",
  ...
  success: function(pictures){
    $.each(pictures, function(idx, picture){
      // picture.pic_location
      // picture.name
      // picture.age
      // picture.gender
    });
  }
});
...

回答by Sarfraz

You can't put multiple echostatements for the AJAX response:

您不能echo为 AJAX 响应放置多个语句:

echo json_encode($url);
echo json_encode($name);
echo json_encode($age);
echo json_encode($gender);

Join your arrays and send a single response:

加入您的数组并发送一个响应:

$arr = $url + $name + $age + $gender;
echo json_encode($arr);

回答by Srinivasan Annamalai

You can easily do this using a single Array:

您可以使用单个数组轻松完成此操作:

$pics = array();

while ($rec = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $pics[$rec['id']]['url'] = $rec['pic_location'];
  $pics[$rec['id']]['name']=$rec['name'];
  $pics[$rec['id']]['age']=$rec['age'];
  $pics[$rec['id']]['gender']=$rec['gender'];
}

echo json_encode($pics);