Jquery Ajax请求不同的页面但提交到同一页面

时间:2016-07-06 14:36:34

标签: javascript php jquery html ajax

我正在开发评论系统,不幸的是我遇到了以下问题:当我调用Ajax将POST数据发送到verify.php页面时,它将数据发送到我调用它的同一页面,称为page.php 。而且,它在控制台中返回以下错误以及当前页面的完整html代码:

  

jquery.min.js:3未捕获的TypeError:a.replace不是函数

以下是我的jquery:

  var logged=<?php if(isset($_COOKIE["user"])){echo "true";}else{echo "false";}?>;
    $(document).ready(function(){
        $(".comments_submit").click(function(){
            if(logged){
                var pid=this.id.split("_")[2];
                var comment=$("#comments_box_"+pid).val();
                if(comment.length>4){
                    $.ajax({
                        url:"verify.php",
                        type:"POST",
                        data:{"add_comment":true,"pid":pid,"comment":comment},
                        success:function(response){
                            console.log(response);
                            try{
                                var resp=JSON.parse(response);
                                if(resp.code=="ADD_COMMENT_SUC"){
                                    $("#comments").prepend($(resp.data).fadeIn('slow'));
                                    $('html, body').animate({scrollTop: $("#comments").offset().top-60}, 500);
                                }
                                else
                                {
                                    toastr.remove()
                                    toastr.error("Server Error! Please Try Again!");
                                }
                            }
                            catch(err){
                                toastr.remove()
                                toastr.error(err);
                            }
                        },
                        error:function(){
                            toastr.remove()
                            toastr.error("Client Error! Please Try Again!");
                        }
                    });
                }
                else
                {
                    $("#comments_box_"+pid).focus();
                    toastr.remove()
                    toastr.error("Please Enter Some Text To Comment!");
                }
            }
            else
            {
                swal("Not Logged In!", "Please login to comment!", "warning");
            }
        });
    });

更新

我的PHP评论系统:

<div class="panel" id="comments_panel">
            <div class="panel-body">

              <div class="comments margin-horizontal-20">
                <h3>Comments</h3>
                <div id="comments">
                <?php
                $sql="SELECT * FROM `comments` WHERE `pid`='$pid' ORDER BY `cid` DESC";
                $result=$conn->query($sql);
                if($result->num_rows>0)
                {
                    while($row=$result->fetch_assoc()){
                        echo '<div class="comment media" id="comment_'.$row["cid"].'">
                                  <div class="media-left">
                                    <a class="avatar avatar-lg" href="javascript:void(0)">
                                      <img src="'.get_gravatar(uid2email($row["uid"])).'" alt="'.uid2username($row["uid"]).'">
                                    </a>
                                  </div>
                                  <div class="media-body">
                                    <div class="comment-body">
                                      <a class="comment-author" href="javascript:void(0)">'.uid2username($row["uid"]).'</a>
                                      <div class="comment-meta">
                                        <span class="date">'.timeago($row["time"]).'</span>
                                      </div>
                                      <div class="comment-content">
                                        <p>'.$row["comment"].'</p>
                                      </div>
                                      <div class="comment-actions">
                                        '.count_likes($row["cid"]).' <i class="icon md-thumb-up comments_like ';if(check_like($row["cid"])){echo 'comments_like_active';} echo '" id="comment_like_'.$row["cid"].'"></i>
                                        '.count_dislikes($row["cid"]).' <i class="icon md-thumb-down comments_dislike ';if(check_dislike($row["cid"])){echo 'comments_dislike_active';} echo '" id="comment_dislike_'.$row["cid"].'"></i>
                                        '.count_sub_comments($row["cid"]).' <i class="icon md-comments comments_comments"></i>
                                      </div>
                                    </div>
                                    <div class="comments">';
                        $sql2="SELECT * FROM `sub_comments` WHERE `cid`='$row[cid]'";
                        $result2=$conn->query($sql2);
                        if($result2->num_rows>0){
                            while($row2=$result2->fetch_assoc()){
                                echo '<div class="comment media">
                                        <div class="media-left">
                                          <a class="avatar avatar-lg" href="javascript:void(0)">
                                            <img src="'.get_gravatar(uid2email($row2["uid"])).'" alt="'.uid2username($row2["uid"]).'">
                                          </a>
                                        </div>
                                        <div class="comment-body media-body">
                                          <a class="comment-author" href="javascript:void(0)">'.uid2username($row2["uid"]).'</a>
                                          <div class="comment-meta">
                                            <span class="date">'.timeago($row2["time"]).'</span>
                                          </div>
                                          <div class="comment-content">
                                            <p>'.$row2["sub_comment"].'</p>
                                          </div>
                                          <div class="comment-actions">
                                            '.count_sub_likes($row2["scid"]).' <i class="icon md-thumb-up sub_comments_like ';if(check_sub_like($row2["scid"])){echo 'sub_comments_like_active';} echo '" id="sub_comment_like_'.$row2["scid"].'"></i>
                                            '.count_sub_dislikes($row2["scid"]).' <i class="icon md-thumb-down sub_comments_dislike ';if(check_sub_dislike($row2["scid"])){echo 'sub_comments_dislike_active';} echo '" id="sub_comment_dislike_'.$row2["scid"].'"></i>
                                          </div>
                                        </div>
                                      </div>';
                            }
                        }
                        else
                        {

                        }
                        echo '<div class="form-group form-material floating">
                                <div class="input-group">
                                    <div class="form-control-wrap">
                                        <textarea class="form-control" id="sub_comments_box_'.$row["cid"].'"></textarea>
                                        <label class="floating-label" for="sub_comments_box_'.$row["cid"].'">Leave A Reply</label>
                                    </div>
                                    <span class="input-group-btn">
                                      <button class="btn btn-default sub_comments_submit" type="button" id="sub_comments_submit_'.$row["cid"].'"><i class="md-comment-edit"></i></button>
                                    </span></div></div></div></div></div>';
                    }
                }
                else
                {
                    echo "Be the first to comment!";
                }
                ?>
                <div class="form-group form-material floating">
                    <div class="input-group">
                        <div class="form-control-wrap">
                            <textarea class="form-control" id="comments_box_<?php echo $pid;?>" rows="3"></textarea>
                            <label class="floating-label" for="comments_box_<?php echo $pid;?>">Add A Comment</label>
                        </div>
                        <span class="input-group-btn">
                          <button class="btn btn-default comments_submit" type="button" id="comments_submit_<?php echo $pid;?>"><i class="md-comment-edit"></i></button>
                        </span>
                    </div>
                </div>
              </div>
              </div>
            </div>
          </div>
          <!-- End Panel Comments -->

0 个答案:

没有答案