I have a form like this:
<form method="post" action="" id="BookingForm">
<label for="email">Email</label>
<input type="text" name="email" id="emailId" value="" />
<input type="hidden" name="action" value="1" />
<input type="submit" name="submitname" value="Send" />
</form>
<div id="container"></div>
My custom js file is in the following path: assets/js/makebooking.js My JS file makebooking.js :
jQuery(document).ready(function(event) {
jQuery('#BookingForm').submit(validateForms);
function validateForms(event) {
event.preventDefault();
var x = MBAjax.ajaxurl;
alert(x);
jQuery.ajax({
action: 'makeBooking',
type: "POST",
url: MBAjax.admin_url,
success: function(data) {
alert(data);
//jQuery("#container" ).append(data);
},
fail: function(error){
alert("error" + error);
}
});
return false;
}
});
Functions.php file:
// embed the javascript file that makes the AJAX request
wp_enqueue_script( 'make-booking-ajax','/wp-content/themes/twentyseventeen/assets/js/makebooking.js', array( 'jquery' ) );
// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( 'make-booking-ajax', 'MBAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
function makeBooking(){
echo "Home again";
return "Home";
}
add_action('wp_ajax_make_booking', 'makeBooking');
The problem is that the ajax call returns all the dom and not what the php function returns. The code in the php function does not work; the echo message is not printed
The image below shows the issue:
Thanks,
Federico
This is my status code and my response of ajax

