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

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

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装