How can I pass the values of a jQuery callback function to a PHP script for further validation? Here, I recieve a latitude and longitude from a getJSON function. I need to use these values to hit the DB wherein they will be compared against existing coordinates to find nearby locations; i.e. a proximity search.
$('#form_map').submit(function() {
var pc = $('#post_code').val();
if(pc != '') {
$.getJSON('/mod/gg.php?pc='+pc, function(data) {
lat = data.results[0].geometry.location.lat;
lng = data.results[0].geometry.location.lng;
});
}
return false;
});
I want to take the values of lat / lng and turn them into php variables. It’s a proximity search. First the user inputed address is turned into coordinates, then it hits the DB through a PHP script. I need the coordinates that were returned from the JSON array to be usable as PHP variables so they can hit the DB.
<?php
if(isset($lat) && isset($lng)){
// Do something
}
Upon receiving the lat / lng values, I want them to be <php $lat = 'Value of callback funcion'; $lng = 'Same'; ?> Then I can run down the rest of the PHP script. How can I do this? Thank you.
First of all: why not store the values in the DB before replying to the ajax request?
If you actually want to do that with a “callback”, you can use a second ajax as the callback to execute your persistancy script.
You must pay attention: php is executed in server-side, javascript is executed client-side. They are both autonomous and the only thing bridging them are HTTP request. And these run asynch. Which means that what you are calling a “callback” will be actually another ajax call to a different script.
I’m just saying this because I got the feeling that you wanted a blocking callback to have linear execution within the same script (and that is not possible). If not ignore this last paragraph