Ajax在执行查询后停止工作
我正尝试使用ajax从php文件加载注释。Ajax在执行查询后停止工作
的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>
';
}
每当我补充一点,$var=$_GET['postid'];
行getcomments.php AJAX脚本停止工作。只要我从getcomments.php中删除$var=$_GET['postid'];
,排除查询部分(显然)窗体显示正确。
任何想法?
在阿贾克斯的数组值更好的设置数据字段:
$.ajax({
type: "get",
url: "getcomments.php",
data: {'getpostcomm':1,'postid':postid},
而在你的PHP,你必须清理你的ID ..(如果INT你可能至少投(INT))...
$var = (int) $_GET['postid'];
此外,在你的PHP加检查,如果isset($ _ GET [ '帖子ID'])...
if(isset($_GET['getpostcomm']) && isset($_GET['postid'])){
现在,这就是我所说的制作ajax的完美方式 – dreamweiver 2013-05-08 07:11:28
这并没有做任何改变。实际上,整个事情是由'$ var =(int)$ _ GET ['getpostcomm'];'一旦我添加这个我得到没有从getcomments.php响应'(alert(“re”);不会工作太) '但是'getcomments.php?getpostcomm = 1&postid = 1'显示所有没有形式的注释。但是当我删除'$ var =(int)$ _ GET ['getpostcomm'];'我从getcomments.php获得响应'(alert(“re”);也可以)'但是'getcomments.php?getpostcomm = 1&postid = 1'只显示形式。 – unkn0wn 2013-05-08 07:37:54
添加mysql_error以查看最新错误... mysql_query()或die(mysql_error()); – Svetoslav 2013-05-08 07:42:28
var dataString = 'getpostcomm=1&postid='+ toString(postid);
“停止工作”不完全是有用的描述......这是什么意思? Anythin在浏览器控制台? http服务器日志中的任何错误?什么有效载荷从服务器发送到服务器? – arkascha 2013-05-08 07:07:36
'$ _GET ['postid'];'这一行导致问题,因为你已经在ajax调用中定义了变量** postid **错误的方式。将ajax调用数据选项从'data:dataString'更改为'data:{pID:dataString},'。 – dreamweiver 2013-05-08 07:08:40
如果你只是去'getcomments.php?getpostcomm = 1&postid = X'会发生什么。你得到的PHP错误? – 2013-05-08 07:09:09