title: Passing a variable with ajax from JS to PHP
body: <p>I am trying to pass a gps coordinate from JavaScript to a php variable on the same page, but after the script ran, and trying to echo in <strong>php</strong>, it doesn't show me anything (Notice: Undefined index: lat).</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
  &lt;p id="gps"&gt;&lt;/p&gt;
  &lt;script&gt;
  var x=document.getElementById("gps");
  var y=document.getElementById("gps");
  function getLocation()
    {
    if (navigator.geolocation)
      {
      navigator.geolocation.getCurrentPosition(showPosition);
      }
    else{x.innerHTML="Geolocation is not supported by this browser.";}
    }
  function showPosition(position)
    {
   x=position.coords.latitude;
    y=position.coords.longitude;
  $.ajax({  
      type: 'POST',
      url: 'index.php', 
      data: { lat: x },
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>PHP code :</p>
  
  <pre><code> echo "&lt;script&gt; getLocation(); &lt;/script&gt;";
          $lat=$_POST['lat'];
          echo $lat;
  </code></pre>
  
  <p>How do I pass a variable from Javascript to php?</p>