Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不使用Wordpress函数提交Wordpress ajax表单可能吗?_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

Php 不使用Wordpress函数提交Wordpress ajax表单可能吗?

Php 不使用Wordpress函数提交Wordpress ajax表单可能吗?,php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,我目前正在Wordpress上创建一个站点,需要通过ajax提交一个表单。不使用Wordpress函数是否可以这样做?我的当前代码没有错误,并且在不更新数据库的情况下返回成功消息。我不明白为什么它不工作请看一下我的简化版本- 这是HTML格式- <form action="" method="post" id="formAppointment" name="appointmentform"> <input type="text" name="message_first_

我目前正在Wordpress上创建一个站点,需要通过ajax提交一个表单。不使用Wordpress函数是否可以这样做?我的当前代码没有错误,并且在不更新数据库的情况下返回成功消息。我不明白为什么它不工作请看一下我的简化版本-

这是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>
$("#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;

});
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);
这是位于process.php中的php

<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>
$("#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;

});
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);
包括“config.php”;
$patientfirstname=htmlspecialchars($\u POST['message\u first\u name']);
$patientlastname=htmlspecialchars($\u POST['message\u last\u name']);
$patientcontactnumber=htmlspecialchars($\u POST['message\u phone']);
$conn=mysqli\u connect($servername、$username、$password、$dbname);
//检查连接
如果(!$conn){
die(“连接失败:”.mysqli_connect_error());
}
$sql=“插入数据_表(firstname、lastname、phonenumber)值(“$patientfirstname”、“$patientlastname”、“$patientcontactnumber”)”;
if(mysqli_查询($conn,$sql)){
echo“新记录创建成功”;
}否则{
echo“Error:”.$sql.
“.mysqli_Error($conn); } mysqli_close($conn);
您必须将数据作为对象传递,而不是作为数据字符串传递

$("#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');    
            }
        });
    }
});

注意:代码中缺少
电子邮件
消息
。因此,行
if(firstname.trim()==“”| | | lastname.trim()==“”| | | | email.trim()==“”| | | message.trim()==“”)可能会引发一些错误,js会跳过剩余代码的执行

在提交ajax请求时,您必须执行
preventDefault()
,为防止表单提交/页面重新加载,页面尚未重新加载,并且正在显示成功消息,但未提交表单数据检查发送的数据是否为,是否为。我的意思是,首先返回您在ajax请求中发送的数据,以便检查您是否在响应中获取了数据,如果您在响应中获取了数据,这意味着ajax请求是正确的,而问题在于您的数据库查询不是ajax请求。请尝试$_get['message_first_name']同样,在post$\u post['message\u first\u name']的位置,它可能会起作用,因为有一次我也遇到了同样的问题。嗯,谢谢,是的,我只是尝试了一下,但没有起作用。谢谢,我更新了我的帖子,但这似乎仍然不起作用,你认为因为它在Wordpress上,所以我需要使用Wordpress的ajax功能吗?你能检查一下控制台有没有任何错误吗?还可以查看一下网络选项卡来分析请求和响应控制台中没有错误,网络选项卡中也没有什么不寻常的东西,我想是的?是否在提交时显示成功警报?有没有其他方法可以检查这一点?您正在告诉我ajax调用已成功完成,并且已收到成功响应。唯一的问题是数据不保存..对吗?