weixin_33694172 2018-09-13 14:06 采纳率: 0%
浏览 9

用AJAX调用表

So Im trying to display a table when the user clicks on a image. I came pretty far, but Im stuck and cant seem to find my mistake.

When I open up my console I get the following error:

enter image description here

Now Hannibal is part of the group of users I want to display in the table.

I wont post the entire code, since its alot. Ill try to keep it to the relevant parts. In this case I know that I get into my .php file that executes the query.

Since I tested the Query, I know it works so Ill just show how I give it back in my separate .php File:

$Guest_Invite_Data = filterTable($List_Guests_Query);
$Guest_Invite_Data_fetched = mysqli_fetch_array($Guest_Invite_Data);

echo json_encode($Guest_Invite_Data_fetched);

function filterTable($query)  
{   
    $filter_Result = mysqli_query($GLOBALS['connect'], $query);
    return $filter_Result;
}    

And heres part of my jQuery:

success:function(Guest_Invite_Data_fetched) {
    createTableByJqueryEach2(Guest_Invite_Data_fetched);
},

And heres my function:

function createTableByJqueryEach2(data2){

    var eTable2="<table><tr><th colspan='5'>Gäste einladen</th></tr><tr><th>Person</th></tr>"
    $.each(data2,function(index2, row2){

        eTable2 += "<tr>";
        $.each(row2,function(key2,value2){
            eTable2 += "<td>"+value2+"</td>";
        });
     eTable2 += "</tr>";
    });
    eTable2 +="</table>";
    $('#guest_table').html(eTable2);
 }

Does anybody spot my mistake?

Edit: Picture of Output as requested

enter image description here

Edit 2: Text of the Output as requested

Array ( [0] => Hannibal [firstname] => Hannibal [1] => Lecter [lastname] => Lecter [2] => Cannibals Inc. [name] => Cannibals Inc. ) {"0":"Hannibal","firstname":"Hannibal","1":"Lecter","lastname":"Lecter","2":"Cannibals Inc.","name":"Cannibals Inc."}
  • 写回答

1条回答 默认 最新

  • weixin_33738578 2018-09-13 15:22
    关注

    Using the following as test code I find two things:

    a. You're fetching both indexed and associative arrays from the database. Note in your JSON the numbered index and value, followed by the associative (column name) and value:

    "0":"Hannibal","firstname":"Hannibal"
    

    You should only fetch what you want. For example, I'd use mysqli_fetch_assoc() here - giving me columns names and values.

    b. You are looping through the data and then trying to loop through the resulting text:, which is what causes the error. For example, row2 on the first iteration contains the value Hannibal, so you second loop is essentially trying to do this:

    $.each('Hannibal',function(key2,value2){...
    

    Hannibal, just being text, cannot be looped through like this. It has no keyss or values.

    You only need one loop for this data. Here is the test code proving this out:

    var guest_data =  {"0":"Hannibal","firstname":"Hannibal","1":"Lecter","lastname":"Lecter","2":"Cannibals Inc.","name":"Cannibals Inc."};
    
    function createTableByJqueryEach2(data2){
    
        var eTable2="<table><tr><th colspan='5'>Gäste einladen</th></tr><tr><th>Person</th></tr>"
        $.each(data2,function(index2, row2){
    
            eTable2 += "<tr>";
            eTable2 += "<td>"+row2+"</td>";
            eTable2 += "</tr>";
        });
        eTable2 +="</table>";
        $('#guest_table').html(eTable2);
     }
    
     createTableByJqueryEach2(guest_data);
    

    This results in the following:

    <div id="guest_table">
        <table>
            <tbody>
                <tr>
                    <th colspan="5">Gäste einladen</th>
                </tr>
                <tr>
                    <th>Person</th>
                </tr>
                <tr>
                    <td>Hannibal</td>
                </tr>
                <tr>
                    <td>Lecter</td>
                </tr>
                <tr>
                    <td>Cannibals Inc.</td>
                </tr>
                <tr>
                    <td>Hannibal</td>
                </tr>
                <tr>
                    <td>Lecter</td>
                </tr>
                <tr>
                    <td>Cannibals Inc.</td>
                </tr>
            </tbody>
        </table>
    </div>
    

    It is likely you want to have a different layout, this is just what I did to prove out the error.

    评论

报告相同问题?

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表