I think I’ve been looking at too much code and have confused myself while trying to get a better understanding of forms in WordPress with AJAX. I set out this week to learn how to create a form for a page and submit it through AJAX.
The page is a template and the action I’ve looked into two solutions of the handling. One through a redirect to a PHP, such as:
<form action="<?php echo get_template_directory_uri() . "/validation.php"; ?>" id="contactForm">
</form>
after reading several posts on the topic that a separate validation of the form should be made. Is this correct and why? Then the other issue is if I do want to process the form on the template-page the form action should look like:
<form action="#" id="contactForm">
</form>
but outside of WordPress I’ve seen:
<form action="" id="contactForm">
</form>
why is this? On the AJAX portion I’ve seen a difference of:
jQuery.ajax({
type :"post",
url: ajaxurl,
}
});
then a different URL of:
jQuery.ajax({
type :"post",
dataType:"json",
url: MBAjax.admin_url,
}
});
and lastly:
jQuery.ajax({
type :"post",
dataType:"json",
url: /wp-admin/admin-ajax.php?action=contactForm",
}
});
So what is the proper way to write the action in WordPress if:
- If the form is processed on the same page?
- If a separate PHP file validates the form?
Then what is the proper AJAX call in WordPress?
Reference points:
- Form submission using AJAX and handling response in WordPress Website
- Submitting HTML form using Jquery AJAX
- Handling an Ajax form submit
- Custom Form with Ajax
- Submitting a form with ajax in WordPress
- submitting a form in wordpress using ajax
- Ajax Form Post Submission Using WordPress
- How To Build Your Own WordPress Contact Form and Why
Edit:
After further reading decided to step into RESTful submissions. I referenced “REST APIs for Absolute Beginners” but I’m not getting the return I’m hoping for:
HTML:
<form id="contactForm" action="" method="POST">
<div class="form-group">
<label for="form_email"><span class="asterisk">*</span>Email address:</label>
<input type="email" class="form-control" id="form_email">
</div>
<div class="form-group">
<button id="form_submit" class="supportbutton" type="submit">Submit</button>
</div>
</form>
<div id="testForm"></div>
jQuery:
$(document).ready(function() {
$('#contactForm').submit(function(e) {
e.preventDefault();
jQuery.ajax({
url: '<?php echo home_url(); ?>/wp-json/darthvader/v1/contact',
type: 'post',
dataType: 'json',
data: $('#contactForm').serialize(),
success: function(data) {
$("#testForm").html(data);
},
error: function() {
alert("There was an issue");
},
});
});
});
functions.php:
add_action('rest_api_init', function() {
register_rest_route('darthvader/v1', '/contact/', array(
'methods' => 'POST',
'callback' => 'darth_contact_form'
));
});
function darth_contact_form(\WP_REST_Request $request) {
$email = $request['form_email'];
return "Your contact request had the title " . $email;
}
Why do I only get Your contact request had the title
on the return and not the email too?