Ajax不提交WordPress表单

时间:2018-09-21 02:18:39

标签: php jquery ajax wordpress

所以,不太确定我在做什么错。我的表单在没有我的Ajax的情况下可以提交到数据库,但是对于我的Ajax则没有。另外,如果对dataString变量执行console.log,则其值将被填充。因此,它将成功提交并触发成功函数,但由于某种原因,由于我的数据库为空,数据永远不会到达我的php函数。另外,控制台中没有错误,并且ajax_url变量也可以正常工作。

var dataString = 'reservationName='+ resName.val() + '&reservationEmail=' + resEmail.val() + '&reservationPhone=' + resPhone.val() + '&reservationTime=' + resTime.val() + '&reservationRequests=' + resReqests.val();

            console.log(dataString);

            $.ajax({
                type: 'POST',
                url: ajax_url,
                data: {
                    'action': 'submit_reservation',
                    from: dataString
                },
                cache: false,
                success: function () {

                    $('form.reservation-form').addClass('submitted');

                    setTimeout(function () {
                        $('form.reservation-form').addClass('hide');
                    }, 400);

                    $('.thanks-message').addClass('appear');

                    resName.val('');
                    resName.removeClass('invalid');
                    resEmail.val('');
                    resEmail.removeClass('invalid');
                    resPhone.val('');
                    resPhone.removeClass('invalid');
                    resTime.val('');
                    resTime.removeClass('invalid');
                    resAS.val('');
                    resAS.removeClass('invalid');

                },
                error: function () {

                    $('form.reservation-form').addClass('submitted');

                    setTimeout(function () {
                        $('form.reservation-form').addClass('hide');
                    }, 400);

                    $('.error-message').addClass('appear');

                    resName.val('');
                    resName.removeClass('invalid');
                    resEmail.val('');
                    resEmail.removeClass('invalid');
                    resPhone.val('');
                    resPhone.removeClass('invalid');
                    resTime.val('');
                    resTime.removeClass('invalid');
                    resAS.val('');
                    resAS.removeClass('invalid');

                }
            });




 <?php

function submit_reservation() {

global $wpdb;

$name = $_POST['reservationName'];
$email = $_POST['reservationEmail'];
$phone = $_POST['reservationPhone'];
$time = $_POST['reservationTime'];
$requests = $_POST['reservationRequests'];
$filter = $_POST['reservationFilter'];
$antiSpam = $_POST['reservationAntiSpam'];

if ($antiSpam === '8') {

    $table = $wpdb->prefix . 'reservations';

    $data = array(
        'name' => $name,
        'phone' => $phone,
        'email' => $email,
        'time' => $time,
        'requests' => $requests
    );

    $format = array(
        '%s',
        '%s',
        '%s',
        '%s',
        '%s'
    );

    $wpdb->insert($table, $data, $format);

}

}

add_action('init', 'submit_reservation');
add_action('wp_ajax_submit_reservation', 'submit_reservation');
add_action('wp_ajax_nopriv_submit_reservation', 'submit_reservation');


?>

1 个答案:

答案 0 :(得分:0)

ajax的数据部分应该具有一组键值对,这些键对具有与$ _POST的键匹配的键。

data: {
        "action"              : "submit_reservation"
        ,"reservationName"    : "a string or variable here"
        ,"reservationEmail"   : aVariable
        ,"reservationPhone"   : anotherVariable
        ,"reservationTime"    : "a string or variable here"
        ,"reservationRequests": "a string or variable here"
        ,"reservationFilter"  : "a string or variable here"
        ,"reservationAntiSpam": "a string or variable here"
      },