数据已插入数据库,但AJAX调用错误函数

时间:2017-10-14 作者:Milos Zivic

我是wordpress的新手,在提交表单时遇到ajax问题。表单被很好地提交,数据被插入到表中,但ajax正在调用一个错误函数。这是我的表格

<form action="#" method="post" id="order-form" novalidate>
                <label for="name"><?php echo __(\'Ime i prezime\', \'mojaobuca\'); ?></label>
                <input type="text" name="name" id="name" class="required" placeholder="<?php echo __(\'Unesite Vaše ime i prezime\', \'mojaobuca\'); ?>">
                <label for="email"><?php echo __(\'Vaša email adresa\', \'mojaobuca\'); ?></label>
                <input type="email" name="email" id="email" class="required" placeholder="<?php echo __(\'Unesite Vašu email adresu\', \'mojaobuca\'); ?>">
                <label for="address"><?php echo __(\'Vaša adresa\', \'mojaobuca\'); ?></label>
                <input type="text" name="address" id="address" class="required" placeholder="<?php echo __(\'Unesite Vašu adresu\', \'mojaobuca\'); ?>">
                <label for="city"><?php echo __(\'Vaš grad\', \'mojaobuca\'); ?></label>
                <input type="text" name="city" id="city" class="required" placeholder="<?php echo __(\'Unesite Vaš grad\', \'mojaobuca\'); ?>">
                <label for="phone"><?php echo __(\'Vaš telefon\', \'mojaobuca\'); ?></label>
                <input type="text" name="phone" id="phone" class="required" placeholder="<?php echo __(\'Unesite Vaš telefon\', \'mojaobuca\'); ?>">
                <label for="shoe-num"><?php echo __(\'Broj obuće\', \'mojaobuca\'); ?></label>
                <input type="text" name="shoe_num" id="shoe_num" class="required" placeholder="<?php echo __(\'Unesite broj obuće\', \'mojaobuca\'); ?>">
                <label for="color"><?php echo __(\'Boja obuće\', \'mojaobuca\'); ?></label>
                <input type="text" name="color" id="color" class="required" placeholder="<?php echo __(\'Unesite boju obuće\', \'mojaobuca\'); ?>">
                <input type="hidden" id="product_id" name="product_id" value="<?php echo get_field(\'obuca_code\'); ?>">
                <?php if(!get_field(\'obuca_discount\')): ?>
                    <input type="hidden" id="shoe_price" name="shoe_price" value="<?php echo get_field(\'obuca_price\'); ?>">
                <?php else: ?>
                    <input type="hidden" id="shoe_price" name="shoe_price" value="<?php echo get_field(\'obuca_discount\'); ?>">
                <?php endif; ?>
                <div class="g-recaptcha" data-sitekey="#"></div>
                <a href="#"
                   name="order-shoe"
                   class="secondary push-down"
                   id="order"
                   ><?php echo __(\'Poruči\', \'mojaobuca\') ?></a>
            </form>
这就是我在函数中注册ajax的地方。php

wp_register_script( \'ajaxscript\', get_template_directory_uri() . \'/js/ajax-script.js\', array(\'jquery\') );
wp_localize_script( \'ajaxscript\', \'myAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' )));
这是我用来插入数据的函数

function mojaobuca_save_order() {
global $wpdb;
$result = \'\';

header(\'Content-Type: application/json\');
$name = sanitize_text_field($_REQUEST[\'name\']);
$email = sanitize_email($_REQUEST[\'email\']);
$address = sanitize_text_field($_REQUEST[\'address\']);
$city = sanitize_text_field($_REQUEST[\'city\']);
$phone = sanitize_text_field($_REQUEST[\'phone\']);
$shoe_num = sanitize_text_field($_REQUEST[\'shoe-num\']);
$color = sanitize_text_field($_REQUEST[\'color\']);
$product_id = sanitize_text_field($_REQUEST[\'product_id\']);
$price = sanitize_text_field($_REQUEST[\'shoe_price\']);
$date = date(\'YYYY-MM-DD HH:MM:SS\');
$table = $wpdb->prefix . \'orders\';

$captcha = $_POST["response"];

$secretKey = \'6LdZ0jMUAAAAAO_rRNXsHrlH2O9oPh4rHkqwkKLE\';
$url = "https://www.google.com/recaptcha/api/siteverify?secret=" . $secretKey . "&response=" . $captcha;
$response = json_decode(file_get_contents($url), true);

if(!$captcha){
    var_dump(\'captcha ne valja\');
    echo \'grecap error\';
}
if($response[\'success\'] == false) {
    var_dump(\'error\');
    echo \'error\';
} else {
    var_dump(\'priprema a upis\');
    $data = array(
        \'name\' => $name,
        \'email\' => $email,
        \'address\' => $address,
        \'city\' => $city,
        \'phone\' => $phone,
        \'number\' => $shoe_num,
        \'color\' => $color,
        \'product_id\' => $product_id,
        \'price\' => $price,
        \'date\' => $date
    );

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

    $wpdb->insert($table, $data, $format);
    $result = \'success\';
}
echo $result;
wp_die();
}add\\u action(\'wp\\u ajax\\u nopriv\\u mojaobuca\\u save\\u order\',\'mojaobuca\\u save\\u order\');add\\u action(“wp\\u ajax\\u mojaobuca\\u save\\u order”,“mojaobuca\\u save\\u order”);

这是我的ajax

$(document).ready(function(){
$(\'#order\').on(\'click\', function(e){
    e.preventDefault();
    $(\'#order-form\').validate();
    var name = $(\'input#name\').val();
    var email = $(\'input#email\').val();
    var address = $(\'input#address\').val();
    var city = $(\'input#city\').val();
    var phone = $(\'input#phone\').val();
    var shoeNum = $(\'input#shoe-num\').val();
    var color = $(\'input#color\').val();
    var shoe_price = $(\'input#shoe_price\').val();
    var product_id = $(\'input#product_id\').val();
    var grecap = $(\'#g-recaptcha-response\').val();
    var response = grecaptcha.getResponse();
    $.ajax({
        type: \'post\',
        url : myAjax.ajaxurl,
        async: false,
        data: {
            name : name,
            email : email,
            address : address,
            city : city,
            phone : phone,
            shoeNum : shoeNum,
            color : color,
            response : response,
            shoe_price: shoe_price,
            product_id: product_id,
            action: "mojaobuca_save_order"
        },
        dataType: \'json\',
        success: function(response) {

            $(\'div.success-msg\').show();
            $(\'#order-form\').hide();
            $(\'#order-form\').resetForm();

        },
        error: function(response) {
            alert(\'error\');
            $(\'div.error-msg\').show();
            $(\'#order-form\').hide();
            $(\'#order-form\').resetForm();
        }
    });
});

1 个回复
最合适的回答,由SO网友:Milos Zivic 整理而成

已解决。在没有数据类型的情况下工作正常:json 在里面ajax-script.jsheader(\'Content-Type: application/json\'); 在php文件中

结束

相关推荐

IS_FROWN_PAGE在函数中不起作用。php

我正在尝试包含一个使用wp\\u enqueue\\u样式的CSS文件,这取决于我是否在头版。我正在使用以下代码测试主页:if ( is_front_page() ) wp_enqueue_style (\'TBDhome\', get_template_directory_uri() . \'/css/TBDhome.css\',\'all\'); 当我在头文件中进行测试并包括CSS文件时,它工作得很好。