Php 将多行选择到数组中,并通过AJAX发送它们

Php 将多行选择到数组中,并通过AJAX发送它们,php,jquery,arrays,ajax,Php,Jquery,Arrays,Ajax,我试图在PHP中运行SELECT查询,然后选择多行,但我需要将它们提取到一个数组中,然后使用:echo json\u encode($array)。在这之后,我需要将这个数组放到AJAX中 以下是PHP代码: $val = $_POST['data1']; $search = "SELECT * FROM employee WHERE emp_name = :val OR salary = :val OR date_employed = :val"; $insertStmt = $conn-&g

我试图在PHP中运行
SELECT
查询,然后选择多行,但我需要将它们提取到一个数组中,然后使用:
echo json\u encode($array)
。在这之后,我需要将这个数组放到AJAX中

以下是PHP代码:

$val = $_POST['data1'];
$search = "SELECT * FROM employee WHERE emp_name = :val OR salary = :val OR date_employed = :val";
$insertStmt = $conn->prepare($search);
$insertStmt->bindValue(":val", $val);
$insertStmt->execute();
$insertStmt->fetchAll();

//echo "success";
//$lastid = $conn->lastInsertId();
$i = 0;
foreach($insertStmt as $row)
{
    $arr[$i] = $row;
    $i++;
}

echo json_encode($arr);
问题是我不能将这个数组的所有行都放到AJAX中,这样我就可以将它们附加到某个表中。以下是脚本:

var txt = $("#txtSearch").val();
$.ajax({
    url: 'search.php', // Sending variable emp, pos, and sal, into this url
    type: 'POST', // I will get variable and use them inside my PHP code using $_POST['emp']
    data: {
        data1: txt
    }, //Now we can use $_POST[data1];
    dataType: "json", // text or html or json or script
    success: function(arr) {
        for() {
            // Here I don't know how to get the rows and display them in a table
        }
    },
    error:function(arr) {
        alert("data not added");
    }
});
你可以回来

json_encode($insertStmt->fetchAll());

此外,请确保仅检索UTF-8或JSON_编码中的字符将“崩溃”

您的成功函数应该如下所示:

success:function(arr)
    {
        $.each(arr,function (i,item) {

           alert(item.YOUR_KEY);
        });
    }

您需要在成功回调中循环“arr”数据。大致如下:

var txt = $("#txtSearch").val();
$.ajax
({
    url: 'search.php', //Sending variable emp, pos, and sal, into this url
    type: 'POST', //I will get variable and use them inside my PHP code using $_POST['emp']
    data: {data1: txt},//Now we can use $_POST[data1];
    dataType: "json", //text or html or json or script

    success:function(arr)
    {
      var my_table = "";
      $.each( arr, function( key, row ) {
            my_table += "<tr>";
            my_table += "<td>"+row['employee_first_name']+"</td>";
            my_table += "<td>"+row['employee_last_name']+"</td>";
            my_table += "</tr>";
      });

      my_table = "<table>" + my_table + "</table>";

      $(document).append(my_table);
    },

    error:function(arr)
    {

        alert("data not added");

    }
    });
var txt=$(“#txtSearch”).val();
$.ajax
({
url:'search.php',//将变量emp、pos和sal发送到此url
键入:'POST',//我将获取变量并使用$\u POST['emp']在PHP代码中使用它们
data:{data1:txt},//现在我们可以使用$\u POST[data1];
数据类型:“json”,//文本或html或json或脚本
成功:功能(arr)
{
var my_table=“”;
$。每个(arr,功能(键,行){
我的桌子+=“”;
我的表格+=“”+行['employee_first_name']+“”;
my_table+=“”+行['employee_last_name']+“”;
我的桌子+=“”;
});
my_table=“”+my_table+”;
$(文档).append(我的表格);
},
错误:函数(arr)
{
警报(“未添加数据”);
}
});

参考其他答案并使用每个答案(:我看不出这是如何回答这个问题的,您已经清理了php,但结果完全相同。这是我的问题。我不知道在
$中写什么。我看到了每个
。我用一个更具体的示例编辑了我的答案,说明了如何使用通过AJAX接收的结果生成HTML表。这就是您想要的吗?我尝试了,但仍然没有改变转到error函数并获取
alert=Data not added
似乎服务器返回的AJAX响应无效,这就是调用“error”回调而不是“success”回调的原因。您可以共享服务器返回的实际AJAX响应吗?如何查看实际AJAX响应,控制台中没有显示任何内容