【问题标题】:Ajax returns array of objects to jquery and phpAjax 将对象数组返回给 jquery 和 php
【发布时间】:2017-06-01 23:43:12
【问题描述】:

我不确定我是否正确地做,但如果有人可以向我解释这个过程,我将不胜感激

我的 main.php 文件中有一个带有 onclick 功能的 div

<div class='show_products' onclick='getData(".$id.")'>

jquery 函数:

function getData(id){    
$.ajax({
    url: 'ajax_info.php',
    type: 'POST',
    dataType:'json',
    data: {id: id},
    success: function(response) {
        console.log(response); //shows a list of objects (array elements)
    }
});
}

还有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文件中创建的数组,返回成功函数内的jquery。

我不明白的是: 如何遍历数组并在 main.php 文件中使用它?我想遍历它并使用数组中的值创建一个表。 意思是 3 列,产品 id - 日期 - 名称,它们将由 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>';
        }
    }
});
}

【问题讨论】:

标签: php jquery arrays ajax


【解决方案1】:

foreach 的每个循环都会删除预览循环中定义的数据,因此当您的foreach 完成迭代时,您最终会得到最后一组数据。

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

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

现在,如果您想从成功函数访问您的数据,您只需将它们与您的行同名即可。例如,如果您的行是 product_id、date 和 name,您将这样称呼它们:

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);
    }
});
}

【讨论】:

  • 您好@john-doh,感谢您的回复。我的主要意思是如何处理 jquery 代码中的数组。这里:成功:函数(响应){}。我在“响应”中得到对象数组
  • 感谢您的帮助@john-doh。我想我设法遍历了数组。只是一个问题,您知道如何将这些数组值添加回表格中的 php 吗?唯一的方法是执行以下操作: document.getElementById('table') 然后在 innerHTML 中添加数据?还是有其他解决方案?
  • 嘿@shieldcy,如果我理解你的话,你想把这些数据复制到你的数据库吗?或者您只是想将一组新数据从 ajax 查询发送到您的 php 页面,以便将其保存在您的 sql 表中?
  • 你好@john-doh,我的意思是,现在我设法通过jquery成功返回函数中的数组......是否有可能以某种方式在main.php文件中添加这些值?我用新的 jquery modificartions 更新了我的第一篇文章。 Main.php 包含 jquery 和 html 内容。我想使用 jquery 回调中的结果在 html 中填充一个表。虽然不确定我说的是否正确。谢谢你的帮助 :)
  • 没关系,我想我可以通过附加表格并添加 来做到这一点。感谢您的帮助
【解决方案2】:

您要做的第一步是将响应解析为 json, 那么你必须用 id 创建 table 标签。

这里是你的 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);
}
});
}

那么这里是html

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

【讨论】:

  • 不错的建议,谢谢。我通过使用非常难看的 InnerHTML 来附加它。你的方法似乎更好。
  • 我认为您希望始终更新表格的内容,因此最好使用 html 然后追加。如果答案对你有帮助,请给它绿色
【解决方案3】:

这里的问题是 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];

    });
    }

【讨论】:

    【解决方案4】:

    您可以在响应上使用 JSON.parse() 函数以使其可供 JavaScript 使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-29
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多