1

Using the HTML5 Geolocation API I've ended up with some variables in Javascript that I need to pass to PHP in order to continue. My code is below, how could it be achieved? I've tried things along the lines of $variable = <script>document.write(variable);</script>;

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8 />
<meta name="viewport" content="width=620" />
<title>test</title>
</head>
<body>
<script type="text/javascript">
//Check if browser supports W3C Geolocation API
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
    }
else    {
    document.write("Geolocation is required for this page.");
    }
function successFunction(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    }
function errorFunction(position) {
    document.write("Error");
    }
</script>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$lat = //TAKE FROM JAVASCRIPT
$lng = //TAKE FROM JAVASCRIPT
$url = "http://api.geonames.org/findNearbyPlaceNameJSON?lat='.$lat.'&lng='.$lng.'&username=demo";
$json = file_get_contents($url);
$data = json_decode($json, true);
$geonames = $data['geonames'][0];
$town = $geonames['name'];
echo "Displaying results near ".$town.". <a href=#>Not in ".$town."?</a>";
?>
</body>
</html>

EDIT: OK, I've done some homework and now I know I'm looking at an AJAX XMLHttpRequest to slick over the two. However, its syntax has slightly thrown me (not to mention the cross-browser issues). Can anyone give me a nudge in the right direction with this one?

6
  • 5
    Why is this question being asked every day at the moment? Take a look at one of the millions of duplicates to the right-hand side, Sebastian. Commented Jul 4, 2011 at 17:27
  • The solutions I tried didn't work and I presumed it was because of the JS functions, which is why my question was specific to this project. Commented Jul 4, 2011 at 17:28
  • possible duplicate of How to use jQuery to make an input's changing value a php variable? Commented Jul 4, 2011 at 17:30
  • All the answers to all those duplicates say "you can't do this", and explain the misunderstanding that you've made. What "didn't work" about that? Commented Jul 4, 2011 at 17:30
  • @Tomalak: I think the solution will be to auto-dupeclose any questions that are double-tagged php/js. Any REAL questions of this sort can go "php js reallyknowwhatImdoing" Commented Jul 4, 2011 at 18:21

3 Answers 3

3

Considering Javascript is interpreted by the client only after the server interprets the PHP, this is impossible.

Your only way to pass the Javascript data to PHP and then show the PHP results would be to send the data retrieved by the Javascript to the PHP via AJAX/XHR and then use Javascript to display the response from the PHP script.

Sign up to request clarification or add additional context in comments.

Comments

1

To pass javascript variables to PHP you will have no choice but to request a new page, since PHP stops executing when a page is sent to the browser.

You can pass to it either via GET, POST or COOKIE. GET is the easiest way: http://domain.com/page?town=XYZ. All you need to do afterwards is store your town in a PHP session and redirect to the desired page using header('Location: http://domain.com/page')

Using an Ajax request will allow you to do that without resorting to sessions and page reloads.

Comments

0

You are mixing Server-side scripting along with browser-side scripting

$variable dissapears on the browser-side and so it will always hold "document.write(variable);" whatever the script inside there is written in.

You can however use AJAX to send the data back to php & process it there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.