I'm trying to pass a wordpress plugin some form data via ajax and it works just fine, unless the form data is serialized, then the server responds with an error message. I'm stuck with this for days now, but I can't get it to work. What am I doing wrong, this can't be so hard, right?
Here's the error message:
call_user_func_array() expects parameter 1 to be a valid callback, function 'process_request' not found or invalid function name in X:\xampp\htdocs\testsite\wp-includes\plugin.php on line 406
The AJAX call:
jQuery(document).ready(function($) {
nonce: whatever
//if i use this variable, it works fine
var data = {action: 'process_request', add_my_data: 'whatever', 'my_data[name]':'whatever', my_nonce: nonce};
//if i use this variable, the server returns the above error .
//because .serialize() doesn't include the submit button's name
//and the form doesn't contain the name of the function to be called, i added them manually to the string. nonce is pulled from form.
var data2 ='action=process_request&add_my_data=whatever&' + $('#my-form').serialize();
$('.my_submit_button').click(function(event) {
event.preventDefault();
jQuery.ajax({
type : 'post',
url : ajaxurl,
timeout: 25000,
data : //data (works) or data2 (doesn't work),
[...]
The strange thing is, the post data for 'data2' seems to be ok and has the same syntax like for 'data'.
I controlled the post data with firebug:
for 'data':
action=process_request&add_my_data=whatever&my_data%5Bname%5D=whatever&my_nonce=1b444dd703
for 'data2' (with the form serialized, the only difference I see is the referer):
action=process_request&add_my_data=whatever&my_data%5Bname%5D=whatever&my_nonce=1b444dd703&_wp_http_referer=%2Ftestsite%2Fadmin%2Ftestpage%2F
The PHP function that handles the request:
function process_request() {
//nonce validation left out for readability
if( isset ($_POST['add_my_data']) ) {
$this->add_info_array('placeholder', 'Database updated');
}
//do some stuff here
die();
}
add_action('wp_ajax_process_request', 'process_request');
UPDATE: The problem is the referer in the string that is created for 'data2'. Check my comment below.
process_request()
is not defined. Where is that function from?