在AJAX查询之后,加载php文件

时间:2013-11-07 18:11:16

标签: php jquery ajax

我这里有一个帖子系统,其中一个人点击“|评论”然后出现一个div,其中包含以下代码。

   <div class="commentbox" id="commentbox<?php echo $_row["id"];?>" style="display:none;"><div style="padding-right:10px;" align="right"><a class="close" href="#" id="<?php echo $_row['id'];?>">X</a></div>
       <div class="scroll-pane">    
          <?php include 'load_comments.php';?>  
        </div>

        <div class="commentupdate"  id='commentbox<?php echo $_row["id"];?>'>

           <div class="stcommenttext" >
                <form method="post" action="">
                     <textarea name="comment" class="comment" maxlength="200"  id="ctextarea<?php echo $_row["id"];?>"></textarea>
                     <br />
                     <input type="submit"  value=" Comment "  id="<?php echo $_row["id"];?>" class="comment_button button"/>
                 </form>
            </div>
        </div>
  </div>

$_row['id']是一个sql fetch数组

Load_comments.php

<?php

   $commentquery = mysql_query("SELECT * FROM comments WHERE msgid=".$_row['id']."");
   $count=mysql_num_rows($commentquery);
      if($count!==0)
      {
         while($commrow = mysql_fetch_array($commentquery))
       {
      ?>
         <div class="stcommentbody" id="stcommentbody<?php echo $commrow['msgid']; ?>">
             <div class="stcommentimg">
             <?php  $userdata = mysql_query('SELECT firstname,lastname,Photo FROM users WHERE id="'.$commrow["uic"].'"' );  
                    $userrow = mysql_fetch_array($userdata); ?>
                    <img src="profile/<?php echo $userrow['Photo']; ?>" class='small_face' alt='<?php echo $userrow['firstname']; echo'&nbsp;';echo $userrow['lastname']; ?>'/>
             </div> 
          <div class="stcommenttext">
        <?php if($commrow['uic']==$uid) { ?>
            <a class="stcommentdelete" href="#" id='<?php echo $commrow['msgid']; ?>' title='Delete Comment'></a>
         <?php } ?>
        <b><a href="profile.php?id=<?php echo $commrow['uic']; ?>"><?php echo $userrow['firstname']; echo'&nbsp;';echo $userrow['lastname']; ?></a></b> <?php echo $commrow['comment'];?>
         <div class="stcommenttime"></div> 
      </div>
   </div>
  <?PHP
    }
  }else{
     echo 'Be the first to comment';
   }
?>

现在,单击提交按钮,将调用以下ajax,

<script>

$(document).ready(function()  {

//Commment Submit

    $('.comment_button').on("click",function(){         

        var ID = $(this).attr("id");

        var comment= $("#ctextarea"+ID).val();
        var dataString = 'comment='+ comment + '&msgid=' + ID;

        if($.trim(comment).length===0)
        {
            alert("Please Enter Comment Text");
        }
        else
        {
            $.ajax({
                type: "POST",
                url: "comment_ajax.php",
                data: dataString,
                cache: false,
                success: function(html){
                  $(".scroll-pane").load("load_comments.php");
                }
            });
        }
        return false;
    });

      // commentopen
    $('.commentopen').on("click",function()
    {
        var ID = $(this).attr("id");
        $("#commentbox"+ID).slideToggle('fast');
        return false;

    });
    $('.close').on("click",function()
    {
        var ID = $(this).attr("id");
        $("#commentbox"+ID).hide('fast');
        return false;

    });
  });  

</script>

ajax运行良好,注释被插入但新注释未加载, 而是错误:

 Notice: Undefined variable: _row in C:\wamp\www\fresh\final\load_comments.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0000  147616  {main}( )   ..\load_comments.php:0

( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\fresh\final\load_comments.php on line 5
Call Stack
#   Time    Memory  Function    Location
1   0.0000  147616  {main}( )   ..\load_comments.php:0
2   0.0030  154344  mysql_num_rows ( )  ..\load_comments.php:5

( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\fresh\final\load_comments.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0000  147616  {main}( )   ..\load_comments.php:0
2   0.0040  154472  mysql_fetch_array ( )   ..\load_commen

请帮我纠正代码 如果不可能,请指导我一个更好的

提前致谢!

2 个答案:

答案 0 :(得分:0)

Load_comments.php依赖于$_row['id'],当您加载Load_comments.php文件时,您将其从上下文中删除,因此您可能会$_row['id']为空

你可能想做这样的事情:

if (!empty($_row['id']))
   $msg_id = $_row['id'];
else if (!empty($_POST['msgid]))
   $msg_id = (int)$_POST['msgid];
$commentquery = mysql_query("SELECT * FROM comments WHERE msgid=". $msg_id ."");

答案 1 :(得分:0)

Load_comments.php而不是

$_row['id']

$_POST['msgid']

所以这条线看起来像这样:

$commentquery = mysql_query("SELECT * FROM comments WHERE msgid=".$_POST['msgid']."");

但请注意,非常易受SQL injection影响。为了避免这种情况,您需要转义所有输入变量,如下所示:

mysql_real_escape_string($_POST['msgid'])

但由于不推荐使用mysql扩展名,请更好地使用PDOmysqli