I am trying to get the results of an ajax request in wordpress, but I get the result "0" in the javascript warning field, 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 is as follows:
$('#ajax-contact-form').submit(function(e){ $.ajax({ data: {action: 'contact_form'}, type: 'post', url: ajaxurl, success: function(data) { alert(data);
And PHP:
add_action('wp_ajax_contact_form', 'contact_form'); add_action('wp_ajax_nopriv_contact_form', 'contact_form'); function contact_form() { echo $_POST['name']; }
Does anyone know if the code above is correct, I also tried $ _REQUEST ['name'] and it does not work.
Thanks a lot,
jquery ajax php wordpress
MariaZ
source share