注意:未定义的索引ajax

时间:2015-05-17 13:54:17

标签: php jquery mysql ajax pdo

所以我有一个工作的zurb-foundation模态,我也有一个ajax代码,每隔x秒刷新一次div。这是代码

的index.php

<a class="th [radius]" href="view-comments.php?ilid=<?= $img['img_id']; ?>" data-reveal-id="viewCommentModal" data-reveal-ajax="true">View</a>

视图-的comments.php

    <script>
    $(document).ready(function(e) {
            setInterval(function() {
                $('#load-comments').load('load-comments.php');
            }, 3000)
        });
    </script>
<body>
    <div id="load-comments"></div>
</body>
<a class="close-reveal-modal">&#215;</a>
</html>

负载的comments.php

<?php
    require 'dbc.php';
    $stmt = $dbc->prepare("SELECT * FROM tbl_comments WHERE img_id=:imageid ORDER by c_id DESC");
    $stmt->bindValue(':imageid', $_GET['ilid']);
    $stmt->execute();
    foreach ($stmt as $data) {
        extract($data);
        echo "<b>{$c_message}</b>";
    }
?>

我的问题是,每次加载load-comments.php时,都会显示错误Undefined index: ilid in D:\wamp\www\instalike\load-comments.php on line 4。我该如何解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用load()传递参数,但您错过了ilid

如果您希望将其作为GET传递,则可以执行此操作

$(document).ready(function(e) {
       var ilid=25; //change ilid as per your need or move it to setInterval
       setInterval(function() {
             $('#load-comments').load('load-comments.php?ilid=ilid');  
         }, 3000)
       });

//负载的comments.php

<?php
    require 'dbc.php';

    if(isset($_GET['ilid'])){
     $stmt = $dbc->prepare("SELECT * FROM tbl_comments WHERE img_id=:imageid ORDER by c_id DESC");
     $stmt->bindValue(':imageid', $_GET['ilid']);
     $stmt->execute();
     foreach ($stmt as $data) {
        extract($data);
        echo "<b>{$c_message}</b>";
     }else{
       echo 'Missing ilid';
      }
    }

?>