AJAX评论系统验证问题



所以我有这个页面,它是显示文章和下面的每篇文章,它将有一个文本区要求允许用户插入评论。我做了AJAX,它工作得很好。一些验证工作得很好(这意味着如果文本区域为空,它将不会提交评论并显示错误)。我这样做验证的方式是与ID。所以我有多种形式与相同的ID。对于提交的注释,它工作得很好,但当我进入第二个表单时,验证不起作用例如,它只适用于第一个表单

AJAX代码

 $(document).ready(function(){
   $(document).on('click','.submitComment',function(e) {
        e.preventDefault();
        //send ajax request
        var form = $(this).closest('form');
        var comment  = $('#comment');
        if (comment.val().length > 1) 
        {
            $.ajax({
                url: 'ajax_comment.php',
                type: 'POST',
                cache: false,
                dataType: 'json',
                data: $(form).serialize(), //form serialize data
                beforeSend: function(){
                    //Changeing submit button value text and disableing it
                    $(this).val('Submiting ....').attr('disabled', 'disabled');
                },
                success: function(data)
                {
                    var item = $(data.html).hide().fadeIn(800);
                    $('.comment-block_' + data.id).append(item);
                    // reset form and button
                    $(form).trigger('reset');
                    $(this).val('Submit').removeAttr('disabled');
                },
                error: function(e)
                {
                    alert(e);
                }
            });
        }
        else
        {
            alert("Hello");
        }
    });
});

index . php

<?php 
    require_once("menu.php");
?>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
        <script  src="comments.js" type="text/javascript" ></script>
<?php
    $connection = connectToMySQL();

    $selectPostQuery = "SELECT * FROM (SELECT * FROM `tblposts` ORDER BY id DESC LIMIT 3) t ORDER BY id DESC";
    $result = mysqli_query($connection,$selectPostQuery)
        or die("Error in the query: ". mysqli_error($connection));
    while ($row = mysqli_fetch_assoc($result)) 
    {
        $postid = $row['ID'];
?>
        <div class="wrapper">
        <div class="titlecontainer">
        <h1><?php echo $row['Title']?></h1>
        </div>
        <div class="textcontainer">
        <?php echo $row['Content']?>
        </div>
<?php
        if (!empty($row['ImagePath'])) #This will check if there is an path in the textfield
        {
?>
            <div class="imagecontainer">
            <img src="images/<?php echo "$row[ImagePath]"; ?>" alt="Article Image">
            </div>
<?php
        }
?>
        <div class="timestampcontainer">
        <b>Date posted :</b><?php echo $row['TimeStamp']?>
        <b>Author :</b> Admin
        </div>
<?php
        #Selecting comments corresponding to the post
        $selectCommentQuery = "SELECT * FROM `tblcomments` LEFT JOIN `tblusers` ON tblcomments.userID = tblusers.ID WHERE tblcomments.PostID ='$postid'";
        $commentResult = mysqli_query($connection,$selectCommentQuery)
            or die ("Error in the query: ". mysqli_error($connection));
        #renderinf the comments
        echo '<div class="comment-block_' . $postid .'">';
        while ($commentRow = mysqli_fetch_assoc($commentResult)) 
        {
?>
            <div class="commentcontainer">
            <div class="commentusername"><h1>Username :<?php echo $commentRow['Username']?></h1></div>
            <div class="commentcontent"><?php echo $commentRow['Content']?></div>
            <div class="commenttimestamp"><?php echo $commentRow['Timestamp']?></div>
            </div>
<?php
        }
?>
        </div>
<?php 
        if (!empty($_SESSION['userID']) ) 
        {
?>
            <form method="POST" class="post-frm" action="index.php" >
            <label>New Comment</label>
            <textarea id="comment" name="comment" class="comment"></textarea>
            <input type="hidden" name="postid" value="<?php echo $postid ?>">
            <input type="submit" name ="submit" class="submitComment"/>
            </form>
<?php
        }
        echo "</div>";
        echo "<br /> <br /><br />"; 
    }
 require_once("footer.php") ?>

问题是第一个表单可以正常工作但第二个表单和其他表单不能正常工作

try this:

var comment  = $('.comment',form);

代替

var comment  = $('#comment');

这样你就锁定了属于你要验证的表单的textarea

ps。

删除元素的id或使其唯一与php,所有的元素的id应该是唯一的

相关内容

  • 没有找到相关文章

最新更新