dreamwind1985 2018-04-23 12:01
浏览 87
已采纳

Wordpress ajax表单提交可能不使用wordpress功能吗?

I'm currently making a site on Wordpress and need a form to be submitted via ajax is it possible to do this without using Wordpress functions? My current code has no errors and returns a success message without updating the database. I don't understand why it's not working please have a look at my simplified version -

This is the form HTML -

<form action="" method="post" id="formAppointment" name="appointmentform">
    <input type="text" name="message_first_name" value="" placeholder="First name" id="appointmentFirstName">
    <input type="text" name="message_last_name" value="" placeholder="Last name" id="appointmentLastName">
    <input type="tel" name="message_phone" value="" placeholder="Phone" id="appointmentPhone">
    <input type="submit" id='appointmentSubmit' class='xAnim' name="submit">
</form>

This is the jquery AJAX -

$("#formAppointment").submit(function(e){
    var firstname    = $("#appointmentFirstName").val();
    var lastname     = $('#appointmentLastName').val();
    var phone        = $('#appointmentPhone').val();
    var dataString = 'message_first_name='+ firstname + '&message_last_name=' + lastname + '&message_phone=' + phone;


    if(firstname.trim() == "" || lastname.trim() == "" || phone.trim() == ""){

        alert('missing information');

         e.preventDefault();  

    } else { 

        // AJAX Code To submit Form.
        $.ajax({
            type: "POST",
            url: "process.php",
            data: dataString,
            cache: false,
            success: function(result){
            console.log(dataString);
            alert('success');

            }
        });
    }
    return false;

});

This is the php located in process.php

include "config.php";

$patientfirstname                = htmlspecialchars($_POST['message_first_name']);
$patientlastname                 = htmlspecialchars($_POST['message_last_name']);
$patientcontactnumber            = htmlspecialchars($_POST['message_phone']);

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

        $sql = "INSERT INTO data_table (firstname, lastname, phonenumber ) VALUES ('$patientfirstname', '$patientlastname', '$patientcontactnumber')";

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

        mysqli_close($conn);
  • 写回答

1条回答 默认 最新

  • douzhang8840 2018-04-23 12:06
    关注

    You have to pass data as object, not as dataString.

    $("#formAppointment").submit(function(e) {
        e.preventDefault();
        var firstname = $("#appointmentFirstName").val();
        var lastname = $('#appointmentLastName').val();
        var phone = $('#appointmentPhone').val();
        // var dataString = 'message_first_name=' + firstname + '&message_last_name=' + lastname + '&message_phone=' + phone;
        var data = {
            "message_first_name": firstname,
            "message_last_name": lastname,
            "message_phone": phone,
        }   
    
        if (firstname.trim() == "" || lastname.trim() == "" || phone.trim() == "") {    
            alert('missing information');     
        } else {    
            // AJAX Code To submit Form.
            $.ajax({
                type: "POST",
                url: "process.php",
                data: data,
                cache: false,
                success: function(result) {
                    console.log(result);
                    alert('success');    
                }
            });
        }
    });
    

    NOTE: You are missing email and message in the code. So the line if(firstname.trim() == "" || lastname.trim() == "" || email.trim() == "" || message.trim() == "") may raise some errors and js skips the execution of remaining code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制