Reputation: 17
I have a script I am trying to adapt. As it, the script executes successfully. However, I am trying to stop the last phase of the script and keep the results in PHP. I have read various posts but am not sure where I am going wrong. I understand that JS is client-side and PHP is server-side; from what I have read, the only methods to pass variables without a refresh is XMLHttp or Ajax.
Right now, the browser uses Javascript to get the geolocation coordinates. It sends these coordinates to a php file where the coordinates are used to get the country name. The country name is then sent back to the Javascript where it is updated in the browser. Everything is good EXCEPT I do not want the Javascript to update the browser with the country name; I want to use the country name further in the PHP file and then echo/print a different return based on additional PHP script. However, I cannot get the PHP file to echo/print the variables - each time it shows the constants but not the value of the country name. (Appears null?)
JS Script:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(GEOprocess, GEOdeclined); }else{
document.getElementById('geo').innerHTML = 'Pricing not available. Please
upgrade your browser or visit the Pricing page.';
}
// this is called when the browser has shown support of
navigator.geolocation
function GEOprocess(position) {
// update the page to show we have the lat and long and explain what we do next
var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.getElementById('geo').innerHTML = 'Latitude: ' + lat + ' Longitude: ' + lng;
// now we send this data to the php script behind the scenes with the GEOajax function
GEOajax("geo.php?latlng=" + position.coords.latitude + "," +
position.coords.longitude);
}
// this is used when the visitor bottles it and hits the "Don't Share" option
function GEOdeclined(error) {
document.getElementById('geo').innerHTML = 'Error: ' + error.message;
}
// this checks if the browser supports XML HTTP Requests and if so which method
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// this calls the php script with the data we have collected from the
geolocation lookup
function GEOajax(url) {
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send();
}
// this reads the response from the php script and updates the page with it's output
function updatePage() {
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
document.getElementById("geo").innerHTML = '' + response;
}
}
The PHP script is:
<?php
$url = 'http://maps.google.com/maps/api/geocode/xml?
latlng='.htmlentities(htmlspecialchars(strip_tags($_GET['latlng']))).'&sensor=true';
$xml = simplexml_load_file($url);
foreach($xml->result->address_component as $component){
if($component->type=='country'){
$geodata['country'] = $component->long_name;
}
}
echo $geodata['country'];
?>
I anticipate that I need to do something in the JS file to stop the update process; I tried to comment out the update parts but that didn't work when I used echo/print in the php file to see the variable data. The current script works but I want to stop the update JS part - I want to keep and use the variables in the PHP file only.
Any help is much appreciated! And I apologize in advance if I am missing something - I did read numerous posts on this and did not find a solution.
Upvotes: 0
Views: 204
Reputation: 670
If you want to keep the country string, you can use the $_SESSION variable
<?php session_start();
/* Your code here */
$geodata['country'] = $_SESSION['country'];
Now you can do whatever you want with the country string by calling $_SESSION['country']. You must prefix each php you wish to use the $_SESSION['country'] variable in with a session_start();.
Upvotes: 1