简体   繁体   中英

Submit button intermittently not submitting form information

So I've got this form for adding comments under a post. The methods utilized here are MYSQL(holds the submitted form data in a database) PHP(communicating with the database) and JavaScript, more specifically AJAX (for hooking up the submit button and handling events). Typing in your comment into the form and pressing submit is supposed to print the comment onto the screen. When I click submit, it doesn't print anything. Then, when I type another comment and click submit once more, it prints the contents of that comment. Other times, it successfully prints the contents of the comment instead of failing to submit. I checked it out in inspect element and in the console log, whenever it misses, it still sends some blank <p> tags through with the class of the comment that should be submitted.

The PHP page for the comment form:

<head>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="Forums.css">
</head>
<body>
    <?php
    $result = mysqli_query($link, $displayPost); ?>
    <?php $row = mysqli_fetch_assoc($result);?>
    <p> <?php echo  $row["title"];?> </p>
    <br>
    <p> <?php echo $row["body"];?> </p>
    <form action="<?php echo $url ?>" method="post" id="form-group">
        <div class="forum col-md-12">
            <textarea type="text" style="overflow: auto; resize: none;" name="body" class="txtBody"></textarea>
            <input type="submit" name="submit" class="btnCreate" style="margin-bottom: 4px;">
        </div>
    </form>
</body>
<script>
    function refreshData() {
        $.ajax({
            type:'GET',
            url: 'getcomments.php?id=<?php echo $id ?>',
            dataType: 'html',
            success: function(result){
                console.log(result);
                $('#comments').html(result);
            }
        });    
    }
    $(document).ready(function () {
        refreshData();
        $("#form-group").submit(function (event) {
            var $form = $(this);
            console.log($form.attr('action'));
            var serializedData = $form.serialize();
            $.ajax({
                url: $form.attr('action'),
                type: 'POST',
                data: serializedData
            });
            refreshData();
            event.preventDefault();
        });
    });
</script>
<div id="comments"></div>

The PHP page for getting previously submitted comments and printing them on the screen

<?php
$link = mysqli_connect("localhost", "root", "WassPord64", "forum");
$id = $_GET["id"];
$displayPost = "SELECT * FROM comments WHERE post_id='$id'";
$link->query($displayPost);
$result = mysqli_query($link, $displayPost);
if (mysqli_num_rows($result) > 0) :
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) :
        $row = mysqli_fetch_assoc($result);?>
        <p class="postBody"><?php echo $row['body'];?></p>
    <?php endwhile; ?>
<?php endif; ?>

You are calling refreshData() when the Ajax is not done. You can make a callback function by using $.ajax.success

Try this:

$(document).ready(function () {
    refreshData();
    $("#form-group").submit(function (event) {
        var $form = $(this);
        console.log($form.attr('action'));
        var serializedData = $form.serialize();
        $.ajax({
            url: $form.attr('action'),
            type: 'POST',
            data: serializedData,
            success: function(){
              refreshData();
            }
        });
        event.preventDefault();
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM