How to pass variables values from .post response to PHP
I'm trying to run Geocode from Google Maps Api with my wp-admin plugin. I
have my own post type where is input for address of place, and jQuery code
watching for click on div. So that's my code:
Plugin Code [PHP]:
add_action( 'wp_ajax_generate_geocode', array($this, 'generate_geocode') );
[...]
//here is my test function that is waiting for response and doing something.
public function generate_geocode() {
print_r($_POST);
die();
}
AJAX Code [JS with jQuery]:
jQuery(document).ready(function() {
var lat = '';
var lng = '';
jQuery('#generate-geocode').on('click', function() {
var address = jQuery('#address').val();
// alert(address);
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: address}, function(results, status) {
//alert(status);
if (status == google.maps.GeocoderStatus.OK) {
lat = results[0].geometry.location.lat();
lng = results[0].geometry.location.lng();
alert(lat + ' ' + lng);
}
});
data = {
action: 'generate_geocode',
lat: lat,
lng: lng
};
jQuery.post(ajaxurl, data, function(response) {
alert(response);
});
});
});
My problem is that I don't know how to pass lat and lng variables or it's
values (value of results[0].geometry.location.lat() and
results[0].geometry.location.lng()) to my PHP code. So for a now I have in
my alert:
Array
(
[action] => generate_geocode
[lat] =>
[lng] =>
)
Can You tell me how to pass that values? And maybe there is better way? :)
No comments:
Post a Comment