07-27-2023, 10:06 AM
I have been wrestling with this problem for quite some time now.
I have an options page for a theme, and a single option registered.
I have been trying to get the option updated via ajax every time a user presses the save button, here is my code.
JS:
function save_main_options_ajax() {
$('.main-options-form').submit( function () {
var b = $(this).serialize(),
optdata = { action : "wp_ajax_main_options_save", data: b };
$.post( ajaxurl, b, function (response) {
if (response == 1 ) { alert('sucess'); }
else { alert(optdata);}
});
return false;
});
}
save_main_options_ajax();
The php:
function main_options_save_ajax() {
check_ajax_referer('_wpnonce', '_wpnonce' );
$data = $_POST;
unset($data['option_page'], $data['action'], $data['_wpnonce'], $data['_wp_http_referer']);
if ( update_option('main_options', $data ) )
{ die(1); }
else { die (0); }
}
add_action('wp_ajax_main_options_save', 'main_options_save_ajax' );
The response i see in firebug is 0. Im not sure what im missing here, i have tried this with some variations but nothing seems to work.
I have an options page for a theme, and a single option registered.
I have been trying to get the option updated via ajax every time a user presses the save button, here is my code.
JS:
function save_main_options_ajax() {
$('.main-options-form').submit( function () {
var b = $(this).serialize(),
optdata = { action : "wp_ajax_main_options_save", data: b };
$.post( ajaxurl, b, function (response) {
if (response == 1 ) { alert('sucess'); }
else { alert(optdata);}
});
return false;
});
}
save_main_options_ajax();
The php:
function main_options_save_ajax() {
check_ajax_referer('_wpnonce', '_wpnonce' );
$data = $_POST;
unset($data['option_page'], $data['action'], $data['_wpnonce'], $data['_wp_http_referer']);
if ( update_option('main_options', $data ) )
{ die(1); }
else { die (0); }
}
add_action('wp_ajax_main_options_save', 'main_options_save_ajax' );
The response i see in firebug is 0. Im not sure what im missing here, i have tried this with some variations but nothing seems to work.