使用Ajax向PHP发布评论

使用Ajax向PHP发布评论,php,jquery,ajax,events,event-delegation,Php,Jquery,Ajax,Events,Event Delegation,我有这个评论表。此评论表单可以正常工作,但在我发布评论时无法正常工作。它可以在一篇文章上工作,在另一篇文章上刷新页面,并且也不会插入数据。所有文章都是通过while循环获取的 JS: $(函数(){ $(“#提交”)。单击(函数(){ var mcomment=$(“输入#mcomment”).val();//定义名称变量 var mesgid=$(“输入#mesgid”).val(); if(mcomment='')//if name字段为空 { 警告(“请写评论”);//警告一个错误消息

我有这个评论表。此评论表单可以正常工作,但在我发布评论时无法正常工作。它可以在一篇文章上工作,在另一篇文章上刷新页面,并且也不会插入数据。所有文章都是通过while循环获取的

JS:


HTML:


你需要做的是

  • 删除提交按钮并添加
  • 第二,在页面中创建一个名为
  • 若这并不能解决问题,那个么发布你们的While循环
更换这条线

 $("#submit").click(function() {


注意:您的提交也可以工作,但是您必须使用jquery停止其默认行为

post的循环在哪里?在此处添加您的全部代码。您可以做的第一件事是将按钮类型从submit更改为button,这样页面刷新将停止,现在可以查看控制台中的错误。将事件参数传递给onclick方法,并使用event.preventDefault阻止提交表单@阿尼基辛格:现在有什么问题?刷新页面?还是显示数据?由于页面刷新问题可以解决,如果你按照我的答案。它张贴整个页面again@AniketSingh:页面刷新已停止?是的,但当我评论时,整个页面都嵌入到帖子中。您当前页面的名称是什么?
<form method="POST" id="commentform">
<div class="panel-footer p15">
    <div class="admin-form">
        <img src="image.png">
        <label for="reply1" >
        <input name="mesgid" id="mesgid" type="hidden" value="
            <?php echo $id ?>">
            <input name="mcomment" id="mcomment" placeholder="Respond with a 
                comment." type="text"  style="width:130%;">
            </label>
            <button type="submit" id="submit" class="button 
                btn-primary submit-btn" name="" style="width:90px; margin-
                left:50px;">Comment</button>
    </div>
</div>
</form>
<?php

$msql=mysql_query("SELECT * from `messages` ORDER BY `msg_id` DESC LIMIT 
$post_limit");



while($messagecount=mysql_fetch_array($msql))
{ 
$id=$messagecount['msg_id'];
$msgcontent=$messagecount['message'];
$usermsg=$messagecount['username'];
$userimg=$messagecount['image'];
$userimg1=$messagecount['user_img'];
$usertime=$messagecount['time'];

?>
<i class="pointer" id="pagination-<?php echo $id;?>"></i>
<div style="display: block;" class="timeline-item" id="clone-3">

      <div class="panel">
        <div class="panel-heading">

          <span class="panel-title" style="color:#000;"><?php echo 
ucfirst($usermsg); ?> Updated a </span><a href="post.php?id=<?php echo 
$id;?>">Post</a>
          <span class="panel-date pull-right mr10 text-muted fs12"> 
<?php echo timeAgo($messagecount['time']);?> via Web</span>
        </div>
        <div class="panel-body">
          <p><img src="image.php/<?php echo $userimg1;?>?width=60&
height=70&nocache&quality=100&image=http://localhost/niftians/profile
/upload/<?php echo $userimg1;?>" />&nbsp; &nbsp; <?php echo 
parse_smileys(make_clickable(nl2br(stripslashes($msgcontent))), 
$smiley_folder); ?><br><br><?php if(!empty($messagecount['image'])) { ?>

      <img src="status/image.php/<?php echo 
$messagecount['image'];?>?width=350&nocache&quality=100&
image=http://localhost/niftians/profile/upload/<?php echo
$messagecount['image'];?>" style="margin-left:10%;">
      <?php } ?></p>

        </div>

      </div>
    </div>

<?php 

$sql=mysql_query("select * from comments where msg_id_fk='$id' order by 
com_id");
$comment_count=mysql_num_rows($sql);

if($comment_count>2)
{
$second_count=$comment_count-2;
?><div class="comment_ui" id="view<?php echo $id; ?>">
<div>
<a href="#" class="view_comments" id="<?php echo $id; ?>">View all <?php 
echo $comment_count; ?> comments</a>
</div>
</div>
<?php 
} 
else 
 {
$second_count=0;
}
?>

<div id="view_comments<?php echo $id; ?>"></div>
<div id="two_comments<?php echo $id; ?>">

<?php
$listsql=mysql_query("select * from comments where msg_id_fk='$id' order 
by com_id limit $second_count,2 ");


while($rowsmall=mysql_fetch_array($listsql))
{ 
$c_id=$rowsmall['com_id'];
$comment=$rowsmall['comments'];
$userid3=$rowsmall['username'];
$userimg5=$rowsmall['user_img'];
$usercom=$rowsmall['time'];

?>  <div id="comment_display"></div>

          <div class="media mt15" id="aniket">
                  <a class="pull-left" href="#"> <img class="media-
object thumbnail thumbnail-sm rounded mw40" src="image.php/<?php echo 
$userimg5;?>?width=60&height=60&nocache&quality=100&
image=http://localhost
/niftians/profile/upload/<?php echo $userimg5;?>" alt="..."> </a>
                  <div class="media-body mb5">
                    <h5 class="media-heading mbn"><a href="<?php echo 
$userid3; ?>"><?php echo ucfirst($userid3); ?></a>
                      <small> -<?php echo timeAgo($rowsmall['time']);?>
</small>
                    </h5>
                    <p><?php echo 
parse_smileys(make_clickable(nl2br(stripslashes($comment))), 
$smiley_folder); ?></p>
                  </div>
                </div>
              <?php } ?>
              </div>

              <form method="POST" id="commentform">
              <div class="panel-footer p15">
                <div class="admin-form">

<img src="image.php/<?php echo $info->img;?>?width=45&nocache&
quality=100&image=http://localhost/niftians/profile/upload/<?php echo
$info->img;?>">
<label for="reply1" >

<input name="mesgid" id="mesgid" type="hidden" value="<?php echo $id ?>">

<input name="mcomment" id="mcomment" placeholder="Respond with a 
comment." type="text"  style="width:130%;"> 

                  </label>
                 <button type="submit" id="submit" class="button 
btn-primary submit-btn" name="" style="width:90px; margin-
left:50px;">Comment</button>
                </div>
              </div>
              </form>
              <br>
<?php
}
?>
 $("#submit").click(function() {
 $("#submit").on('.submit','click',function(e) { 
 e.preventDefault();