Suivez la variable "formdata" et regardez le code de support pour voir comment cela fonctionne dans un environnement wordpress.
J'utilise ce côté client (js):
// bind button, setup and call .ajax
jQuery('#submitbutton').click(function() {
jQuery('#response_area').html('');
// put all name-values from form into one long string
var serializedformdata = jQuery('#submitform').serialize();
// configure array of options for the ajax call (can use a different action for each form)
options = {
type: 'POST',
url: sv_submitform_global_js_vars.ajaxurl,
datatype: 'json',
data: {
clienttime: getnow(),
sv_submit_form_check: jQuery('#sv_submit_form_check').val(),
// this data:action:'value' is specifically required by the wordpress wp_ajax_<value> action hook to process the received data on the php/server side
action: 'sv_submitform_hook',
formdata: serializedformdata,
},
beforeSend: beforesendfunc,
// process returned json formatted data in function named below
success: successfunc,
}
// execute the ajax call to server (sending data)
jQuery.ajax(options);
});
... et ce sur le côté serveur (PHP) pour récupérer les données sur et dans un joli tableau associatif pour le serveur travail de base de données de côté.
/////////////////////////////////////
// ajax serverside data handler ///
/////////////////////////////////////
// Add AJAX actions for submit form
// Serves logged in users
add_action('wp_ajax_sv_submitform_hook', 'sv_submitform_handler');
// Serves non-logged in users
add_action('wp_ajax_nopriv_sv_submitform_hook', 'sv_submitform_handler');
// this is the function that processes the input from the submit form
function sv_submitform_handler(){
date_default_timezone_set('EST');
$servertime = date('h:i:s a').' Server Time';
// fda = form data array can be used anywhere after the next statement.
// example: if ($fda['formfieldbyname'] == 'something'){...};
parse_str($_POST['formdata'],$fda);
// this is how the nonce value is read
// form side is wp_nonce_field('sv_submitform','sv_submitform_check');
if (!check_ajax_referer('sv_submitform', 'sv_submitform_check', false)){
$data = $servertime . ' (Security Failed)';
} else {
$data = $servertime . ' (Security Passed)';
};
$returndata = array('data' => $data);
exit(json_encode($returndata));
};
Et pour les codeurs WordPress là-bas, il m'a fallu un certain temps pour se rendre compte que le crochet wp_ajax_
devait être dans un fichier de plug-in ou le functions.php de mon thème enfant. Cela ne fonctionnera pas dans un gabarit de page normal!
Merci c'est ce que je cherchais :) –
Rappelez-vous que JSON est un sous-ensemble de Javascript - si votre objet Javascript a des fonctions, alors ils ne peuvent pas être stockés dans JSON. Si votre objet contient uniquement des hachages/tableaux/données, JSON est suffisant. –
qu'en est-il de json_decode? http://php.net/manual/fr/function.json-decode.php –