I am using WordPress and cannot figure what to do with the serialized data from Ajax after it is sent over. I have read on this site that parse_str
is what I need, but I am unsure how to make use of it.
Here is the jQuery for the Form Submit
jQuery( document ).ready( function( $ ) {
$( '#log_data' ).submit( function( event ) {
event.preventDefault();
console.log( $( this ).serialize() );
var data = $(this).serialize();
action = 'my_submit_log_action';
$.post(
ajaxurl,
data,
function ( response ) {
if ( ! response.success ) {
alert( 'Failure!' );
}
alert( 'Success!' );
}
);
});
});
Because this is in WordPress, I have to pass an action so WordPress knows what function to pass this data to. I am not sure if I am passing the action right (see above).
The second part is the PHP, which is what I do not understand. How do I take the serialized data and post it to the database?
add_action('wp_ajax_my_submit_log_action', 'my_submit_log_action');
add_action('wp_ajax_nopriv_my_submit_log_action', 'my_submit_log_action');
function my_submit_log_action() {
global $wpdb;
$user_id = $_POST['user_id'];
$length = $_POST['length'];
$ground = $_POST['ground'];
$date = $_POST['date'];
$notes = $_POST['notes'];
$wpdb->insert('wp_jo_plugin_options', array (
'user_id' => $user_id,
'length' => $length,
'ground' => $ground,
'date' => $date,
'notes' => $notes,
) );
die();
}