AJAX帖子不起作用

时间:2016-12-01 20:56:38

标签: php ajax

我试图在数据库中使用AJAX发布数据,但没有任何反应。我检查了开发人员工具是否有任何错误,但没有出现错误,并且发送请求成功发送

我正常测试代码以便在没有AJAX的情况下发布数据并且运行良好,但是使用AJAX没有任何事情发生

enter image description here

HTML:

<form id="form_box" action="" method="post">

    <h5>Body:</h5>
    <textarea name="body" cols="30" rows="10"></textarea>

    <div>
        <input type="submit" name="sub_post" value="Post Ajax">
    </div>

</form>

JS:

    <script
        src="https://code.jquery.com/jquery-1.12.4.min.js"
        integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
        crossorigin="anonymous"></script>

<script>
    // Variable to hold request
    var request;

    // Bind to the submit event of our form
    $("#form_box").submit(function(event){

        // Abort any pending request
        if (request) {
            request.abort();
        }
        // setup some local variables
        var $form = $(this);

        // Let's select and cache all the fields
        var $inputs = $form.find("input, select, button, textarea");

        // Serialize the data in the form
        var serializedData = $form.serialize();

        // Let's disable the inputs for the duration of the Ajax request.
        // Note: we disable elements AFTER the form data has been serialized.
        // Disabled form elements will not be serialized.
        $inputs.prop("disabled", true);

        // Fire off the request to /form.php
        request = $.ajax({
            url: "/functions/post.php",
            type: "post",
            data: serializedData
        });

        // Callback handler that will be called on success
        request.done(function (response, textStatus, jqXHR){
            // Log a message to the console
            console.log("Hooray, it worked!");
           // alert("Post Added successfully");
        });

        // Callback handler that will be called on failure
        request.fail(function (jqXHR, textStatus, errorThrown){
            // Log the error to the console
            console.error(
                "The following error occurred: "+
                textStatus, errorThrown
            );
        });

        // Callback handler that will be called regardless
        // if the request failed or succeeded
        request.always(function () {
            // Reenable the inputs
            $inputs.prop("disabled", false);
        });

        // Prevent default posting of form
        event.preventDefault();
    });

</script>

PHP:

<?php
include_once('../includes/config.php');

$post=$db->real_escape_string($_POST['body']);
$db->query("insert into ajax(a_post) values ('$post')");




?>

1 个答案:

答案 0 :(得分:0)

我解决了: jquery代码应包含$(document).ready()处理程序

$(document).ready(function(){
        // Variable to hold request
        var request;

    // Bind to the submit event of our form
    $("#form_box").submit(function(event){

        // Abort any pending request
        if (request) {
            request.abort();
        }
        // setup some local variables
        var $form = $(this);

        // Let's select and cache all the fields
        var $inputs = $form.find("input, select, button, textarea");

        // Serialize the data in the form
        var serializedData = $form.serialize();

        // Let's disable the inputs for the duration of the Ajax request.
        // Note: we disable elements AFTER the form data has been serialized.
        // Disabled form elements will not be serialized.
        $inputs.prop("disabled", true);

        // Fire off the request to /form.php
        request = $.ajax({
            url: "functions/post.php",
            type: "post",
            data: serializedData
        });

        // Callback handler that will be called on success
        request.done(function (response, textStatus, jqXHR){
            // Log a message to the console
            console.log("Hooray, it worked!");
            // alert("Post Added successfully");
        });

        // Callback handler that will be called on failure
        request.fail(function (jqXHR, textStatus, errorThrown){
            // Log the error to the console
            console.error(
                "The following error occurred: "+
                textStatus, errorThrown
            );
        });

        // Callback handler that will be called regardless
        // if the request failed or succeeded
        request.always(function () {
            // Reenable the inputs
            $inputs.prop("disabled", false);
        });

        // Prevent default posting of form
        event.preventDefault();
    });
    })