简体   繁体   English

使用 Jquery Ajax PHP 提交表单

[英]Form Submit with using Jquery Ajax PHP

I know there is a lot of similar questions but I am working on this exact same problem for two days and I just gave up.我知道有很多类似的问题,但我在这个完全相同的问题上工作了两天,我只是放弃了。

So after the form is submitted, I want to prevent the current page (updates.php) to redirect to another page (test.php).所以在表单提交后,我想阻止当前页面(updates.php)重定向到另一个页面(test.php)。 I am trying to do this with Jquery Ajax, but in this point, I am open to any solution.我正在尝试使用 Jquery Ajax 来做到这一点,但在这一点上,我愿意接受任何解决方案。

updates.php:更新.php:

<form action="test.php" method="post">                          
    <div class="row form-group">
        <div class="col-md-12">
            <label class="sr-only" for="name">Name</label>
            <input type="text" id="name" name="name" class="form-control" style="background:white;opacity:.5;border:none;" placeholder="Name:" required>
        </div>
    </div>

    <input type = "hidden" id="id" name = "id" value = "4" />

    <div class="row form-group">
        <div class="col-md-12">
            <label class="sr-only" for="subject">Comment</label>
            <input type="text" name="subject" id="subject" class="form-control" style="background:white;opacity:.5;border:none;" placeholder="Write a comment..." required>
        </div>
    </div>
    <div class="form-group">                                
        <input type="submit" value="Post Comment" class="btn btn-primary">                                                                          
    </div>
</form>

test.php:测试.php:

<?php

$id = $_POST['id'];
$username = $_POST['name'];
$comment = $_POST['subject'];

if(!empty($username) || !empty($comment))
{

    $conn = mysqli_connect('localhost','Admin','admin123','website');

    if(!$conn)
    {
        echo "Connection Error: " . mysqli_connect_error();
    }
    else
    {
        $INSERT = "INSERT INTO comments (id, name, comment) VALUES (?,?,?)";

        $stmt = $conn -> prepare($INSERT);
        $stmt -> bind_param("iss", $id, $username, $comment);
        $stmt -> execute();
    }

}
else { echo "All fields are required"; die();}

?>

Whatever I did I couldn't stop test.php to open.无论我做了什么,我都无法停止打开 test.php。

Try this as your updates.php file instead:试试这个作为你的 updates.php 文件:

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script>
            function submitWithAjax(){
                var name = document.getElementById("name").value;
                var id = document.getElementById("id").value;
                var subject = document.getElementById("subject").value;

                $.post( "test.php", {name: name, id: id, subject: subject})
                .done(function(data) {
                    alert( "Data Loaded: " + data );
                });
            }
        </script>
    </head>
    <body>
        <form>                          
            <div class="row form-group">
                <div class="col-md-12">
                    <label class="sr-only" for="name">Name</label>
                    <input type="text" id="name" name="name" class="form-control" style="background:white;opacity:.5;border:none;" placeholder="Name:" required>
                </div>
            </div>

            <input type = "hidden" id="id" name = "id" value = "4" />

            <div class="row form-group">
                <div class="col-md-12">
                    <label class="sr-only" for="subject">Comment</label>
                    <input type="text" name="subject" id="subject" class="form-control" style="background:white;opacity:.5;border:none;" placeholder="Write a comment..." required>
                </div>
            </div>
            <div class="form-group">                                
                <input type="submit" value="Post Comment" class="btn btn-primary" onclick="event.preventDefault();submitWithAjax();">                                                                          
            </div>
        </form>
    </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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