JSON数据发布但是表格没有填充

时间:2013-01-16 14:38:02

标签: php mysql ajax json pdo

编辑:

  1. 更改了Jquery代码,以便动态重新创建行,但仍然不行。
  2. 重组我的桌子还是不行。
  3. 我有一个php脚本,将我的db表编码为一个数组。我回应json_encode,它回声很好。脚本:

    <?php
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password="testdbpass"; // Mysql password
    $db_name="test"; // Database name
    
    // Connect to server via PHP Data Object
    $dbh = new PDO("mysql:host=localhost;dbname=test", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $array = $dbh->query("SELECT id, anum, first, last,
                            why, comments, aidyear, additional_req,
                            signintime FROM inoffice WHERE 
                            counselorname IS NULL")->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($array);
    
    ?>
    

    然后我有一个页面应该提取数据然后将其放入表中。出于某种原因,我无法将其发布到该页面上的第一个表格中。

    这些是我在页面上运行的jquery脚本:

        <head>
        <script src="core/media/js/jquery.js" type="text/javascript"></script>
        <script src="core/media/js/jquery.dataTables.js" type="text/javascript"></script>
        <script type="text/javascript" src="core/media/js/install.js"></script>
        <script type="text/javascript">
        $.ajax({
        url: 'core/media/js/getdatainoffice.php',
        type: 'GET',
        async: false,
        dataType: 'json',
        success: function (result) {
    
            var insert = '';
    
            $.each(result, function() {
                insert += '<tr><td>' + id + '</td><td>' + anum + '</td><td>' + first + '</td><td>' + last + '</td><td>' + why + 
                            '</td><td>' + comments + '</td><td>' + additional_req + '</td><td>' + aidyear + '</td><td>' 
                            + signintime + '</td></tr>';
            });
            $('#datatables tr').after(insert);
        }
    
    
    });
    
        </script>    
    

    我是否需要在每个表数据的jquery ajax中创建一个数组,或者根据我的理解,我认为json处理数组编码的方式。

    这是我的表:

     table id='datatables' class='display'>
                    <thead>
                        <tr>
                     <th>Session ID </th>                        
                     <th>A Number</th>
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Reason for visit</th>
                            <th>Comments</th>
                            <th>Aid Year</th>
                            <th>Additional Requirements</th>
                            <th>Signin Time</th>
                        </tr>
                    </thead>
                        <tbody>
    
    
                    </tbody>
            </table>
    

    任何帮助或解释都会很可爱。我一直在读这个site,但无济于事。

    JSON数组:

      

    [{ “ID”: “7”, “ANUM”: “B00000000”, “第一”: “rixhers”, “最后一个”: “ajazi”, “为什么”: “其他”, “注释”:”突出部分   需要一些   帮助! “ ”additional_req“: ”“, ”aidyear“: ”12-13“, ”signintime“:” 2013年1月16日   9时08分35秒 “},{” ID “:” 8" , “ANUM”: “A00000000”, “第一”: “rixhers”, “最后的”: “ajazi”, “为什么”: “上诉”,”评论 “:” “ ”additional_req“: ”“, ”aidyear“: ”12-13“, ”signintime“:” 2013年1月16日   9时28分57秒 “},{” ID “:” 9" , “ANUM”: “A00000000”, “第一”: “rixhers”, “最后的”: “ajazi”, “为什么”: “上诉”,”评论 “:” “ ”additional_req“: ”“, ”aidyear“: ”12-13“, ”signintime“:” 2013年1月16日   10时12分07秒 “},{” ID “:” 10" , “ANUM”: “A00000000”, “第一”: “rixhers”, “最后的”: “ajazi”, “为什么”: “上诉”,”评论 “:” “ ”additional_req“: ”“, ”aidyear“: ”12-13“, ”signintime“:” 2013年1月16日   11时19分18" 秒}]

    更多信息:我使用的是一个名为datatables的jquery插件,我需要填充该表。

1 个答案:

答案 0 :(得分:2)

                $.(#datatables).append(data)

在你运行它时,data是一个数组。你不能只是将一些随机的javascript数据结构附加到DOM中 - 你必须从该数组中的数据实际构建表行。 e.g。

html = ''
for (x in data) {
   html += '<tr><td>' + data['somefield'] + '</td></tr>';
}
$('#datatables').append(html);

如果您对这些数据所做的只是把它放到一个html表中,那么用PHP在服务器上构建html并将其作为单个字符串推送到网上可能会更容易。

相关问题