Languages: English • Português do Brasil • 日本語 (Add your language)
Send a JSON response back to an AJAX request, and die().
<?php wp_send_json( $response ) ?>
jQuery(document).ready(function(){
jQuery('#btn_save').click(function(e){
e.preventDefault();
jQuery.post(pluginUrl+'ajax/save_field.php',jQuery('#my-form').serialize(), function(data) {
alert(data.message + " ID:" + data.ID);
//This will alert "Saved ID:1"
});
});
});
save_field.php
<?php
// .................
// nonce checks ....
// .................
$return = array(
'message' => 'Saved',
'ID' => 1
);
wp_send_json($return);
wp_send_json() is located in wp-includes/functions.php