0

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' );

Wordpress Post Request

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);
1
  • Hi. A) and what is your problem? B) why do you put that esc_attr everywhere? You’re not printing any html attributes, so using esc_attr in this code doesn’t make any sense... Feb 19 '19 at 6:33
0

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.

And back to main problem...

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.

1
  • Thank you for the detailed response. It was certainly my main issue, and things work now! Feb 20 '19 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.