Im trying read form POST values submitted over ajax call (admin-ajax.php).The POST is prepared and submitted. Here are the server side action hooks I have in Child Theme's functions.php
add_action('save_post', 'address_save_postdata',1,1);
add_action( 'wp_ajax_wilcity_handle_review_listing',
'address_save_postdata' );
add_action( 'wp_ajax_wilcity_handle_review_listing', 'address_save_postdata' );
Here is how I am trying to read the form post values under the callback function:
if ( esc_attr($_POST['action']) == "wilcity_handle_review_listing") {
$lat_val = esc_attr($_POST['data[tmbu_location][group][tmbu_lat]']);
$lng_val = esc_attr($_POST['data[tmbu_location][group][tmbu_lng]']) ;
$address_val = esc_attr($_POST['data[tmbu_location][group][tmbu_address]']) ;
$cityname_val = esc_attr($_POST['data[tmbu_location][group][tmbu_cityname]']) ; }
else
{
$lat_val = esc_attr($_POST['tmbu_lat']);
$lng_val = esc_attr($_POST['tmbu_lng']) ; $address_val = esc_attr($_POST['tmbu_address']) ; $cityname_val =
esc_attr($_POST['tmbu_cityname']) ;
}
update_post_meta($post_id, '_tmbu_lat', $lat_val);
update_post_meta($post_id, '_tmbu_lng', $lng_val); update_post_meta($post_id, '_tmbu_address', $address_val);
update_post_meta($post_id, '_tmbu_cityname', $cityname_val);
Im trying read form POST values submitted over ajax call (admin-ajax.php).The POST is prepared and submitted. Here are the server side action hooks I have in Child Theme's functions.php
add_action('save_post', 'address_save_postdata',1,1);
add_action( 'wp_ajax_wilcity_handle_review_listing',
'address_save_postdata' );
add_action( 'wp_ajax_wilcity_handle_review_listing', 'address_save_postdata' );
Here is how I am trying to read the form post values under the callback function:
if ( esc_attr($_POST['action']) == "wilcity_handle_review_listing") {
$lat_val = esc_attr($_POST['data[tmbu_location][group][tmbu_lat]']);
$lng_val = esc_attr($_POST['data[tmbu_location][group][tmbu_lng]']) ;
$address_val = esc_attr($_POST['data[tmbu_location][group][tmbu_address]']) ;
$cityname_val = esc_attr($_POST['data[tmbu_location][group][tmbu_cityname]']) ; }
else
{
$lat_val = esc_attr($_POST['tmbu_lat']);
$lng_val = esc_attr($_POST['tmbu_lng']) ; $address_val = esc_attr($_POST['tmbu_address']) ; $cityname_val =
esc_attr($_POST['tmbu_cityname']) ;
}
update_post_meta($post_id, '_tmbu_lat', $lat_val);
update_post_meta($post_id, '_tmbu_lng', $lng_val); update_post_meta($post_id, '_tmbu_address', $address_val);
update_post_meta($post_id, '_tmbu_cityname', $cityname_val);
OK, so there are many problems with your code... But I'm pretty sure I know, where the major one lies...
But first things first...
You add your AJAX callbacks with this code:
add_action( 'wp_ajax_wilcity_handle_review_listing', 'address_save_postdata' );
add_action( 'wp_ajax_wilcity_handle_review_listing', 'address_save_postdata' );
It doesn't make sense to add the same function twice. It can be a mistake, but maybe you forgot to add nopriv
to the second line.
There are arrays sent in the POST request and you're trying to access them like so:
$_POST['data[tmbu_location][group][tmbu_lng]']
It won't work. 'data[tmbu_location][group][tmbu_lng]
is not a string key. It's an array with three keys. It means, that you should access it like so:
$_POST['data']['tmbu_location']['group']['tmbu_lng']
PS. And one more thing. You've put esc_attr
everywhere in your code. But it doesn't make much sense, I'm afraid. esc_attr
is a function that does the escaping for HTML attributes. You don't print any html attributes anywhere in your code. You don't have to escape string before comparing them or saving them to DB and in most case you shouldn't do so.