forms - Reading POST over admin-ajax.php

admin2025-06-03  3

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);
Share Improve this question edited Feb 19, 2019 at 7:23 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 19, 2019 at 2:08 Morgan Janjua CraneMorgan Janjua Crane 55 bronze badges 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... – Krzysiek Dróżdż Commented Feb 19, 2019 at 6:33
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748891459a314583.html

最新回复(0)