weixin_33698043 2017-05-22 05:24 采纳率: 0%
浏览 29

表单使用ajax php提交

submitting a simple contact form using ajax.

EDITED CODE now trying to insert the data to database.

html form

        <form align="left" method="post" class="subscribe_form" action='subscribe.php'> 

                    Your Name:<br>
  <input type="text" name="name" value="" required><br>

Your E-Mail:<br>
  <input type="email" name="email" value="" required><br><br>

  <p>    <input name="gender" value="male" type="radio" id="male" />
      <label for="male">Male</label>

      <input name="gender" value="female" type="radio" id="female" />
      <label for="female">Female</label>

Company Name:
<input type="text" name="cname" value="" required><br><br>
<input type="submit" name="send" value="Subscribe" id="subscribe"> <span class="output_result"></span>

this is my ajax code :


$(document).ready(function() {

        // Add text 'loading...' right after clicking on the submit button. 

        var form = $(this);
            dataType: "text",
            data: form.serialize(),
            success: function(result){
                if (result == 'success'){
                    $('.output_result').text('thank you!');  
                } else {

        // Prevents default submission of the form after clicking on the submit button. 
        return false;   


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rm";

    $name = $_POST['name'];
    $gender = $_POST['gender'];
    $email = $_POST['email'];
    $cname = $_POST['cname'];
    $sub_date = date("Y-m-d");

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());

$sql = "insert into rmsubscribe (name, gender, email, cname, sub_date) values ('$name', '$gender','$email','$cname','$sub_date')";
$result = (mysqli_query($conn, $sql));

echo ($result) ? 'success' : 'error'; */


this code give me 'error message'.

if i use without ajax code like this,

at form ,

without the 'class="subscribe_form"..

and in subscribe.php

 if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);

instead of this

$result = (mysqli_query($conn, $sql));
echo ($result) ? 'success' : 'error'; 

it works fine. 'New record created successfully' data inserted into table.

Please help me on ajax code. i am not familiar with ajax. how to make this work using ajax?

  • 写回答

1条回答 默认 最新

  • 喵-见缝插针 2017-05-22 08:45

    I think you need to add datatype to your ajax request. Actually your ajax request is expecting json and you are returning text - "Success" that is not json that's why when it gets unexpected data returned it is sending ajax response to error function. Update you code to below ajax request -

    As you are returning Text as response to your ajax request so your ajax request datatype must be text as below line of code in ajax request -

    dataType: "text"

    Example of your expected Ajax Request

                dataType: "text",
                data: form.serialize(),
                success: function(result){
                    if (result == 'success'){
                        $('.output_message').text('Message Sent Successfully!');  
                    } else {
                        $('.output_message').text('Error Sending email!');
            // Prevents default submission of the form after clicking on the submit button. 
            return false;   



    • ¥15 yolov5双目识别输出坐标代码报错
    • ¥15 这个代码有什么语法错误
    • ¥15 给予STM32按键中断与串口通信
    • ¥15 使用QT实现can通信
    • ¥15 关于sp验证的一些东西,求告知如何解决,
    • ¥35 关于#javascript#的问题:但是我写的只能接码数字和字符,帮我写一个解码JS问题
    • ¥15 prophet运行报错,如何解决?
    • ¥15 用GPU跑pytorch搭建的LSTM的时候出现了奇怪的报错
    • ¥20 前端数据是从session等作用域拿到的,如何取值继续传递后端呢
    • ¥15 eclipse无法正常运行