在Word preSS提交表单与AJAX [英] Submitting a form with ajax in Wordpress

查看:144
本文介绍了在Word preSS提交表单与AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的话preSS一个Ajax请求的结果,但我在JavaScript中的一个警告框,让'0',所以形式如下:

I am trying to get the results of an ajax request in wordpress, but I am getting result of '0' in an alert box of javascript, so the form looks like this:

<form class="form" id="ajax-contact-form" action="#">                            
        <input type="text" name="name" id="name"  placeholder="Name" required="">
        <button type="submit" class="btn">Submit</button>
</form>

中的JavaScript看起来是这样的:

The javascript looks like this:

$('#ajax-contact-form').submit(function(e){

    $.ajax({ 
         data: {action: 'contact_form'},
         type: 'post',
         url: ajaxurl,
         success: function(data) {
              alert(data); // This prints '0', I want this to print whatever name the user inputs in the form. 

        }
    });

})

和PHP的:

add_action('wp_ajax_contact_form', 'contact_form');
add_action('wp_ajax_nopriv_contact_form', 'contact_form');

function contact_form()
{
echo $_POST['name'];    
}

有谁知道,如果code以上是正确的,我也尝试$ _REQUEST ['名称'],它不工作。

Does anyone know if the code above is correct, I have also tried $_REQUEST['name'] and it doesnt work.

感谢洙多,

推荐答案

尝试是这样的:

$('#ajax-contact-form').submit(function(e){
    var name = $("#name").val();
    $.ajax({ 
         data: {action: 'contact_form', name:name},
         type: 'post',
         url: ajaxurl,
         success: function(data) {
              alert(data); //should print out the name since you sent it along

        }
    });

});

这篇关于在Word preSS提交表单与AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆