AJAX表单不能与php一起使用

时间:2015-08-02 00:54:41

标签: php jquery ajax

我正在使用AJAX和PHP来处理表单,我已经尝试了3个小时,我无法弄清楚为什么它不会将数据插入到mysql服务器中。

与服务器的连接正常。 PHP和html表单在我将其组合到一个页面时起作用。这告诉我它是AJAX,但AJAX看起来很好。我真的不知道这里发生了什么。请帮帮我。

用于处理代码的php:

<?php
require_once 'config/config.php';
function test_input($data){
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

$que1 = $_POST['que1'];

$sql = "INSERT INTO question_answers 
(
    user_id, que1, que2
)
VALUES 
(
    '99' ,'$que1', '3345'
)";
$conn->close(); 
?>

AJAX就在这里:

$(document).ready(function() {
    $("#submit1").click(function() {
        var que1 = $('#que1').val();
        if (que1 == '')
        {
            alert("Please provide a response."); 
        }
        else {
            $.ajax({
                type: "POST",
                url: "process.php",
                data: {
                    que1: que1
                },
                success: function(msg) {
                    alert($que1);
                    var url = "process.php";
                    $(location).attr('href', url);
                },
                error: function() {
                    alert('Error');
                }
            });
        } 
    });
});

我的表格是标准的:

<form action="" method="POST">  
    Question
    <textarea class="form-control" name="que1" rows="3" id="que1"></textarea>
    <button type="submit" class="btn btn-primary" id="submit1" name="submit1">Submit response</button>
</form>

1 个答案:

答案 0 :(得分:0)

科学你的问题我还不清楚,但我的代码中仍然有一些错误。在你的php文件中:

    <?php
    require_once 'config/config.php';
    function test_input($data){
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }

    $que1 = $_POST['que1'];

    $sql = "INSERT INTO question_answers 
    (
        user_id, que1, que2
    )
    VALUES 
    (
        '99' ,'$que1', '3345'
    )";

    /*
     You did not run the query here. 
     Execute the query here and try 
     to echo something as ajax response.
    */
    if (mysql_query($sql))//or mysqli or PDO whatever you use in your connection.
    {
       echo 'success';
    }
    else
    {
       echo 'Error';
    }
    $conn->close(); 
    ?>

在您的Ajax代码中:

$(document).ready(function() {
    $("#submit1").click(function() {
        var que1 = $('#que1').val();
        if (que1 == '')
        {
            alert("Please provide a response."); 
        }
        else {
            $.ajax({
                type: "POST",
                url: "process.php",
                data: {
                    que1: que1
                },
                success: function(msg) {
                    //alert($que1);
                    //var url = "process.php";
                    //$(location).attr('href', url);
                    alert(msg);
                },
                error: function() {
                    alert('Error');
                }
            });
        } 
    });
});

检查警报是否显示正确的消息。