Quantcast
Channel: PHP Website Development » PH
Viewing all articles
Browse latest Browse all 11

After submitting how can I pass JS variables to following PH

$
0
0

After a user fills his address in a text field and hits the submit button the address is sent to google maps by javascript which then receives back the coordinates of the address. I then want to pass the coordinates back to a following PHP code so I can save them to the DB. So if this is the code under a single PHP file how can this be done?
P.S. Just to be clearer, all of this is done under one PHP function that handles the submitting.
...
var lat = results[0].geometry.location.latitude;
var lng = results[0].geometry.location.longitude;
...
}

$latitude = ??? ;
$longitude = ??? ;
?>
…………………………………..

If you get the lat/long in the page when submit is clicked – before the form is submitted to you your handling PHP script – just store the results in hidden inputs;

using js to update their values after you receive them from the API call;
$(“lat”).value = results[0].geometry.location.latitude;Then allow the form to submit as normal and fetch “lat” from the query string/post data.
…………………………………..

Not possible, PHP executes before the client receives the file (after which the user’s browser executes the JavaScript)
What you want can easily be achieved by using AJAX. Have the JavaScript call an external php file which would simply get the variables it receives and inserts it into the database. For example in your ajax request, have the url be the address to your external php file with the variables appended to the uri query:
“storedata.php?latitude=”+lat+”&longitude=”+lngThen in storedata.php:
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];

// process them here and insert into your database .etc


Viewing all articles
Browse latest Browse all 11

Trending Articles