间歇性提交按钮,不提交表单信息

时间:2018-11-19 20:22:49

标签: javascript php jquery ajax

因此,我有此表单可用于在帖子下添加评论。这里使用的方法是MYSQL(将提交的表单数据保存在数据库中)PHP(与数据库进行通信)和JavaScript,更具体地说是AJAX(用于挂接提交按钮和处理事件)。 在表单中输入评论,然后按提交,即可将评论打印到屏幕上。 当我单击提交时,它不会打印任何内容。然后,当我键入另一个评论并再次单击提交时,它将打印该评论的内容。有时,它会成功打印注释的内容,而不是提交失败。 我在inspect元素和控制台日志中将其检出,无论何时丢失,它仍会发送一些空白的<p>标签以及应提交的注释类。

注释表单的PHP页面:

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

PHP页面,用于获取先前提交的注释并将其打印在屏幕上

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

1 个答案:

答案 0 :(得分:3)

未完成Ajax时,您正在呼叫Private Sub Form_Current() If (Me.NewRecord = True) Then Dim lookupNum As Integer txtEventID = gblEventID lookupNum = DLookup("Max(Subplot_Num)", "tbl_Subplots", "Event_ID = " & txtEventID.Value) + 1 If (IsNull(lookupNum)) Then txtSubplotNum = "1" Else txtSubplotNum = lookupNum End If End If If xboPoaching.Value = False Then txtPoachingNotes.Enabled = False End Sub 。您可以使用$ .ajax.success

进行回调

尝试一下:

refreshData()
相关问题