Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax将对象数组返回给jquery和php_Php_Jquery_Arrays_Ajax - Fatal编程技术网

Ajax将对象数组返回给jquery和php

Ajax将对象数组返回给jquery和php,php,jquery,arrays,ajax,Php,Jquery,Arrays,Ajax,我不确定我做的是否正确,但如果有人能向我解释一下这个过程,我将不胜感激 我在main.php文件中有一个带有onclick函数的div <div class='show_products' onclick='getData(".$id.")'> 还有ajax文件 $sql = "SELECT ......"; $results = Db::getInstance()->ExecuteS($sql); foreach($results as $row) { $produ

我不确定我做的是否正确,但如果有人能向我解释一下这个过程,我将不胜感激

我在main.php文件中有一个带有onclick函数的div

<div class='show_products' onclick='getData(".$id.")'>
还有ajax文件

$sql = "SELECT ......";
$results = Db::getInstance()->ExecuteS($sql);
foreach($results as $row) {
    $product_id = $row['product_id'];
    $date = $row['date'];
    $name = $row['name'];

    $array_info['product_id'] = $product_id;
    $array_info['date'] = $date;
    $array_info['name'] = $name;
}

echo json_encode($array_info);
上面的代码将在ajax文件中创建的数组返回给success函数中的jquery

我不明白的是: 如何遍历数组并在main.php文件中使用它?我想遍历它并创建一个包含数组中的值的表。 表示3列,product id-date-name,它们将由ajax返回的数组填充

这是可能的还是我做错了

非常感谢您的帮助

编辑: 更新的jquery代码

function getData(id){    
$.ajax({
    url: 'ajax_info.php',
    type: 'POST',
    dataType:'json',
    data: {id: id},
    success: function(response) {
        document.getElementById('table_'+id).style.display='block';

        for ( var i = 0; i < response.length; i++ ) {
            console.log(response[i]);                
            var div = document.getElementById('table_'+id);
            div.innerHTML = div.innerHTML + '<tr><td>'+response[i].product_id+'</td><td></td><td></td><td></td><td></td></tr>';
        }
    }
});
}
函数getData(id){
$.ajax({
url:'ajax_info.php',
键入:“POST”,
数据类型:'json',
数据:{id:id},
成功:功能(响应){
document.getElementById('table_'+id).style.display='block';
对于(变量i=0;i
您的
foreach
的每个循环都将删除预览循环中定义的数据,因此当您的
foreach
完成迭代时,您将得到最后一组数据

您不需要遍历数组,因为您的查询已经返回了一个数组

$sql = "SELECT ......";
$results = Db::getInstance()->ExecuteS($sql);
echo json_encode($results);
现在,如果您想从success函数访问数据,只需调用与行相同的名称即可。例如,如果您的行是product\u iddatename,您将这样称呼它们:

function getData(id){    
$.ajax({
    url: 'ajax_info.php',
    type: 'POST',
    dataType:'json',
    data: {id: id},
    success: function(response) {
        response = JSON.parse(response);

        console.log("Product id is: " + response[0].product_id);
        console.log("Product name is: " + response[0].name);
        console.log("Product date: " + response[0].date);
    }
});
}
您还需要了解以下内容:假设出于某种原因,您希望在不更改数据库的情况下为json使用不同的变量名,那么您必须执行以下操作:

$sql = "SELECT ......";
$results = Db::getInstance()->ExecuteS($sql);
$newResults = array();

foreach ($results as $row)
{
    $newResults[] = array('newId' => $row['product_id'], 'newName' => $row['name'], 'newDate' => $row['date']);
}

echo json_encode($newResults);
更改JSON中的变量将导致:

function getData(id){    
$.ajax({
    url: 'ajax_info.php',
    type: 'POST',
    dataType:'json',
    data: {id: id},
    success: function(response) {
        response = JSON.parse(response);

        console.log("Product id is: " + response[0].newId);
        console.log("Product name is: " + response[0].newName);
        console.log("Product date: " + response[0].newDate);
    }
});
}

这里的问题是javascript不允许关联数组,因此必须使用常规数字数组或对象

以下是使用数字数组的示例:

$sql = "SELECT ......";
$results = Db::getInstance()->ExecuteS($sql);
foreach($results as $row) {
    $product_id = $row['product_id'];
    $date = $row['date'];
    $name = $row['name'];

    $array_info[0] = $product_id;
    $array_info[1] = $date;
    $array_info[2] = $name;
}

echo json_encode($array_info);
然后你可以得到如下输出:

function getData(id){    
$.ajax({
    url: 'ajax_info.php',
    type: 'POST',
    dataType:'json',
    data: {id: id},
    success: function(response) {

      var product_id = response[0];
      var date = response[1];
      var name = response[2];

    });
    }

您要做的第一步是将响应解析为json, 然后您必须创建带有id的表标记

下面是您的JS示例

function getData(id){    
$.ajax({
    url: 'ajax_info.php',
type: 'POST',
dataType:'json',
data: {id: id},
success: function(response) {
    var data = JSON.parse(response);
    var st = "";
    $.each(data, function(index){
       st += "<tr><td>"+data[index].product_id+"</td>";
       st += "<td>"+data[index].date+"</td>";
       st += "<td>"+data[index].name+"</td></tr>";
    });
    $("#table_id").html(st);
}
});
}
函数getData(id){
$.ajax({
url:'ajax_info.php',
键入:“POST”,
数据类型:'json',
数据:{id:id},
成功:功能(响应){
var data=JSON.parse(响应);
var st=“”;
$。每个(数据、函数(索引){
st+=“”+数据[索引]。产品id+“”;
st+=“”+数据[索引]。日期+“”;
st+=“”+数据[索引]。名称+“”;
});
$(“#表格id”).html(st);
}
});
}
然后是html

<table border="1" id="table_id">
</table>


您可以在响应中使用JSON.parse()函数,使其可被JavaScript使用。

谢谢@Mohamed Yousef,我将通过itHello@john doh,谢谢您的回复。我的主要意思是如何处理jquery代码中的数组。这里:success:function(response){}。我得到了“响应”中的一系列对象,感谢您的帮助@john doh。我想我成功地遍历了数组。只有一个问题,您知道如何将这些数组值添加回表中的php吗?唯一的方法是:document.getElementById('table'),然后在innerHTML中添加数据?或者还有其他解决方案吗?嘿@shieldcy如果我理解你的意思,你想把这些数据复制到你的数据库中吗?或者您只是想将一组新的数据从ajax查询发送到php页面,以便它将其保存在sql表中?Hello@john doh,我的意思是,现在我已经在jquery成功返回函数中遍历了数组。。。是否可以在main.php文件中添加这些值?我用新的jquery修改更新了我的第一篇文章。php包含jquery和html内容。我想使用jquery回调中的结果在html中填充一个表。但我不确定我说的是否正确。谢谢你的帮助,还有:)没关系,我想我可以通过追加表格和添加内容来完成。谢谢你的帮助很好的建议,谢谢。我是通过使用非常难看的InnerHTML添加它的。您的方式似乎更好。我认为您希望始终更新表的内容,因此最好使用html而不是append。如果答案有帮助,请给它绿色
<table border="1" id="table_id">
</table>