2013-05-08 32 views
0

我正嘗試使用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'];,排除查詢部分(顯然)窗體顯示正確。

任何想法?

+0

「停止工作」不完全是有用的描述......這是什麼意思? Anythin在瀏覽器控制檯? http服務器日誌中的任何錯誤?什麼有效載荷從服務器發送到服務器? – arkascha 2013-05-08 07:07:36

+0

'$ _GET ['postid'];'這一行導致問題,因爲你已經在ajax調用中定義了變量** postid **錯誤的方式。將ajax調用數據選項從'data:dataString'更改爲'data:{pID:dataString},'。 – dreamweiver 2013-05-08 07:08:40

+0

如果你只是去'getcomments.php?getpostcomm = 1&postid = X'會發生什麼。你得到的PHP錯誤? – 2013-05-08 07:09:09

回答

0

在阿賈克斯的數組值更好的設置數據字段:

$.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'])){ 
+0

現在,這就是我所說的製作ajax的完美方式 – dreamweiver 2013-05-08 07:11:28

+0

這並沒有做任何改變。實際上,整個事情是由'$ 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

+0

添加mysql_error以查看最新錯誤... mysql_query()或die(mysql_error()); – Svetoslav 2013-05-08 07:42:28

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