$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>forms - Reading POST over admin-ajax.php|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

forms - Reading POST over admin-ajax.php

matteradmin8PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far