0

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

索引.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>

获取评论.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>
';

}

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

有任何想法吗?

4

2 回答 2

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'])){
于 2013-05-08T07:10:11.703 回答
0
var dataString = 'getpostcomm=1&postid='+ toString(postid);
于 2013-05-08T07:19:36.280 回答