title: Javascript variable in PHP
body: <p>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 <code>$variable = &lt;script&gt;document.write(variable);&lt;/script&gt;;</code></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;meta charset=utf-8 /&gt;
  &lt;meta name="viewport" content="width=620" /&gt;
  &lt;title&gt;test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;script type="text/javascript"&gt;
  //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");
      }
  &lt;/script&gt;
  &lt;?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.'&amp;lng='.$lng.'&amp;username=demo";
  $json = file_get_contents($url);
  $data = json_decode($json, true);
  $geonames = $data['geonames'][0];
  $town = $geonames['name'];
  echo "Displaying results near ".$town.". &lt;a href=#&gt;Not in ".$town."?&lt;/a&gt;";
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>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?</p>