如何使用JQuery $ .getJSON将数组从PHP文件发布到表中?

时间:2022-01-18 00:24:50

I am trying to output an array to a table, based on a user entered variable called firstname. If two arrays have the same firstname value the table should display both arrays. Currently, only the first array is being displayed in the table. I have included my current code below:

我试图根据用户输入的名为firstname的变量将数组输出到表中。如果两个数组具有相同的firstname值,则表应显示两个数组。目前,只有第一个数组显示在表中。我在下面列出了我当前的代码:

$(document).ready(function() {  
    var url = "data.php";

    $('#getJSON').click(function(event) {
        event.preventDefault();
        var firstname = $('#firstname').val();
        url = url + "?firstname=" + firstname;

        $.getJSON(url, function(json) {
            var tr;
            for (var i = 0; i < json.data.length; i++) {
                tr = $("<tr/>");
                tr.append("<td>" + json.data[0].id + "</td>");
                tr.append("<td>" + json.data[0].title + "</td>");
                tr.append("<td>" + json.data[0].firstname + "</td>");
                $('table').append(tr);
            }
        });
    });
});

PHP script:

PHP脚本:

if ($firstname === "Emma")
    $arr[] = array('id'=> "1", 'title'=> "Miss", 'firstname'=> "Emma", 'surname'=>"Brown");
else if ($firstname === "Emma")
        $arr[] = array('id'=> "2", 'title'=> "Mrs", 'firstname'=> "Emma", 'surname'=>"Green");
else
        $arr[] = array('id'=> "Unknown", 'title'=> "Unknown", 'firstname'=> "Unknown", 'surname'=> "Unknown");

echo '{"firstname":'.json_encode($arr).'}';

I would like it to output like so:
如何使用JQuery $ .getJSON将数组从PHP文件发布到表中?

我希望它输出如下:

Any help is much appreciated!

任何帮助深表感谢!

2 个解决方案

#1


2  

You have two mistakes:

你有两个错误:

First one is the use of the if clause. Since you are using if elseif you cannot put the same condition in both if and elseif.The else if means if previous condition is false then check another condition. In this case the elseif will never be executed since the first condition is true.

第一个是使用if子句。因为你正在使用if else,所以你不能在if和elseif中使用相同的条件。else if表示如果先前条件为假,则检查另一个条件。在这种情况下,由于第一个条件为真,因此永远不会执行elseif。

Second you are always storing data in the same index of your results array so you will overwrite your data and always get one element in this array.

其次,您始终将数据存储在结果数组的相同索引中,以便覆盖数据并始终在此数组中获取一个元素。

$id=1;
$arr=array();

if ($firstname === "Emma"){
    $arr[] = array('id'=> $id++, 'title'=> "Miss", 'firstname'=> "Emma", 'surname'=>"Brown");
    $arr[] = array('id'=> $id++, 'title'=> "Mrs", 'firstname'=> "Emma", 'surname'=>"Green");
 }

 else if($firstname === "NotEmma")
     $arr[] = array('id'=> $id++, 'title'=> "Miss", 'firstname'=> "NotEmma", 'surname'=>"xx");

 else
        $arr[] = array('id'=> "Unknown", 'title'=> "Unknown", 'firstname'=> "Unknown", 'surname'=> "Unknown");

echo '{"firstname":'.json_encode($arr).'}';

#2


0  

You could try this:

你可以试试这个:

var tr;
for (var i = 0; i < json.data.length; i++) {
    tr += "<tr>";
    tr += "<td>" + json.data[i].id += "</td>";
    tr += "<td>" + json.data[i].title += "</td>";
    tr += "<td>" + json.data[i].firstname += "</td>";
    tr += "</tr>";

}

$('table').html(tr);

#1


2  

You have two mistakes:

你有两个错误:

First one is the use of the if clause. Since you are using if elseif you cannot put the same condition in both if and elseif.The else if means if previous condition is false then check another condition. In this case the elseif will never be executed since the first condition is true.

第一个是使用if子句。因为你正在使用if else,所以你不能在if和elseif中使用相同的条件。else if表示如果先前条件为假,则检查另一个条件。在这种情况下,由于第一个条件为真,因此永远不会执行elseif。

Second you are always storing data in the same index of your results array so you will overwrite your data and always get one element in this array.

其次,您始终将数据存储在结果数组的相同索引中,以便覆盖数据并始终在此数组中获取一个元素。

$id=1;
$arr=array();

if ($firstname === "Emma"){
    $arr[] = array('id'=> $id++, 'title'=> "Miss", 'firstname'=> "Emma", 'surname'=>"Brown");
    $arr[] = array('id'=> $id++, 'title'=> "Mrs", 'firstname'=> "Emma", 'surname'=>"Green");
 }

 else if($firstname === "NotEmma")
     $arr[] = array('id'=> $id++, 'title'=> "Miss", 'firstname'=> "NotEmma", 'surname'=>"xx");

 else
        $arr[] = array('id'=> "Unknown", 'title'=> "Unknown", 'firstname'=> "Unknown", 'surname'=> "Unknown");

echo '{"firstname":'.json_encode($arr).'}';

#2


0  

You could try this:

你可以试试这个:

var tr;
for (var i = 0; i < json.data.length; i++) {
    tr += "<tr>";
    tr += "<td>" + json.data[i].id += "</td>";
    tr += "<td>" + json.data[i].title += "</td>";
    tr += "<td>" + json.data[i].firstname += "</td>";
    tr += "</tr>";

}

$('table').html(tr);