I've setup a function and some jQuery that takes a value from a form a user submits, and sends it to the database successfully in it's own table. However, I'm trying to set it up to save the data as JSON instead (so as to make sure my WP table doesn't balloon to an unbearable size in the future).
Function here:
function search_modifications_callback() {
// Ensure we have the data we need to continue
if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {
header( 'HTTP/1.1 400 Empty POST Values' );
echo 'Could Not Verify POST Values.';
exit;
}
$user_id = get_current_user_id(); // Get our current user ID
$search_term = $_POST['saved_search'];
update_user_meta( $user_id, 'saved_search', $search_term ); // Add our user meta
wp_update_user( array(
'ID' => $user_id
) );
exit;
}
add_action( 'wp_ajax_nopriv_search_ss', 'search_modifications_callback' );
add_action( 'wp_ajax_search_ss', 'search_modifications_callback' );
jQuery here:
// Form submission listener
jQuery( '#save-search' ).click( function() {
// Grab our post meta value
var ss_val = jQuery( '#save_search_term' ).val();
var user_id = jQuery( '#user_id' ).val();
// Do very simple value validation
if( jQuery( '#save_search_term' ).val() ) {
jQuery.ajax( {
url : ajax_url,
type: 'POST',
data: {
action : 'search_ss',
id: user_id,
'saved_search': ss_val,
}
} )
.success( function( results ) {
console.log( 'User Meta Updated!' );
alert('Search saved!');
} )
.fail( function( data ) {
console.log( data.responseText );
console.log( 'Request failed: ' + data.statusText );
} );
} else {
}
return false; // Stop our form from submitting
} );
I understand that instead of using 'update_user_meta' I should probably create an object instead, but am unsure how to properly set that up from here.
Open to other suggestions as well. Thanks!