dongtuwu8548 2013-11-07 18:11
浏览 83
已采纳

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

I have here a posting system in which a person clicks on " | Comment " And then a div appears which has the following code in it .

   <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'] is an sql fetch array

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';
   }
?>

Now , On clicking the submit button , The following ajax is called in ,

<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>

The ajax works well , the comment gets inserted but the new comment does not get loaded , instead the error :

 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

please help me to correct the code If not possible , please guide me with a better one

Thanks in advance !

  • 写回答

2条回答 默认 最新

  • dongze8698 2013-11-07 18:20
    关注

    Load_comments.php is depended on $_row['id'] , when you load the Load_comments.php file you take it out of context, so you probably get $_row['id'] empty

    you may want to do somthing like this :

    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条)

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题