Ajax在执行查询后停止工作

时间:2013-05-08 07:04:12

标签: php ajax

我正在尝试使用ajax从php文件加载评论。

的index.php

<div id="commentsonpost" value="<?php echo $_GET['post'];?>">
</div> 

<script type="text/javascript">
$(document).ready(function() { 
    var postid = $('#commentsonpost').attr("value");
    alert(postid);
    var dataString = 'getpostcomm=1&postid='+ postid;
    $.ajax({
                type: "get",
                url: "getcomments.php",
                data: dataString,
                dataType:'html',
                cache: false,
                success: function(html){
                        alert("re");
                     $("#commentsonpost").append(html);


          }
         });

    return false;
});
</script>

getcomments.php

if(isset($_GET['getpostcomm'])){


$var=$_GET['postid']; //  Adding this line causing problems

$querycomm = "select U.fname,U.lname,U.usernick,C.bcommentid,C.comment,C.date,C.visible from blogcomments as C natural join users as U where C.visible=1 and U.visible=1 and C.bpostid='{$var}' ORDER BY C.date ASC";

$resultcomm = mysql_query ( $querycomm, $connection );
echo "<div id='pcomments'>";
while($commentonpost=mysql_fetch_array($resultcomm)){
    if($commentonpost['visible']==1){
        echo '
        <div style="width:90%;float:left;margin-left:5%;margin-right:15%;margin-top:10px;" id="comment'.$commentonpost['commentid'].'">

        <div style="width:10%;float:left;"><a href="profile.php?user='.$commentonpost['usernick'].'"  >'.$commentonpost['fname']." ".$commentonpost['lname'].'</a></div>
        <div style="width:78%;float:left;margin-left:2%;">'.$commentonpost['comment'].'</div>
        <div style="width:8%;float:right;margin-left:2%;">
        ';
        if($commentonpost['usernick']==$_SESSION['user_nick']){
            echo '  <form action="" method="post">
            <input type="submit"  name="delcomm" value="X" class="delcombutton" id="'.$commentonpost['commentid'].'">

            </form>
            ';
        }
        echo '<h5 class="msg">'.datetime($commentonpost['date']).'</h5>
        </div>
        <br/>
        </div>

        ';
    }
}
echo "</div>";




echo '
<form name = "form" method = "post" action=""  onsubmit="return validateform()" style="width:100%">
<div style="width:90%;float:left;margin-left:5%;margin-right:15%;margin-top:10px;">

<div style="width:10%;float:left;"><a href="profile.php?user='.$_SESSION['user_nick'].'"  >'.$_SESSION['user_fname']." ".$_SESSION['user_lname'].'</a></div>
<div style="width:78%;float:left;margin-left:2%;"><textarea placeholder="Comment..." name="commenttext" id="commenttext" class="inputcomment" ></textarea></div>

<br>
<input type="submit" id="'.$_POST['post'].'" name="SubmitComment" value="Comment " class="commentbutton" style="font-size:1em;width:100px;float:right;margin-top:4px;margin-right:9%;">
</div>
</form>
</div>
';

}

每当我在getcomments.php中添加$var=$_GET['postid'];行时,ajax脚本就会停止工作。只要我从getcomments.php中删除$var=$_GET['postid'];,就会正确显示查询部分(显然)表单。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

在ajax更好地将数据字段设置为数组值:

$.ajax({
type: "get",
url: "getcomments.php",
data: {'getpostcomm':1,'postid':postid},

在你的PHP中,你必须清理你的Id ..(如果它的int你至少可以施放(int))......

$var = (int) $_GET['postid'];

另外在你的PHP添加检查是否isset($ _ GET ['postid'])...

if(isset($_GET['getpostcomm']) && isset($_GET['postid'])){

答案 1 :(得分:0)

var dataString = 'getpostcomm=1&postid='+ toString(postid);