title: Pass JS variable to new PHP file
body: <p>I am using Google Maps to autocomplete and convert an address to Latitude &amp; Longitude. I can show it in a DIV but the problem is that when I try to pass this variable to PHP to store it in database it doesn't work. I tried passing it with AJAX and failed. I have also tried to directly pass it to PHP with POST. I cannot get this variable to send it to database. </p>
  
  <p>Here is the code:</p>
  
  <pre><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
        &lt;script src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;libraries=places"&gt;&lt;/script&gt;
            &lt;script&gt;
  
  
          function initialize() {
                var address = (document.getElementById('pac-input'));
                var autocomplete = new google.maps.places.Autocomplete(address);
                autocomplete.setTypes(['geocode']);
                google.maps.event.addListener(autocomplete, 'place_changed', function() {
                    var place = autocomplete.getPlace();
                    if (!place.geometry) {
                        return;
                    }
  
                var address = '';
                if (place.address_components) {
                    address = [
                        (place.address_components[0] &amp;&amp; place.address_components[0].short_name || ''),
                        (place.address_components[1] &amp;&amp; place.address_components[1].short_name || ''),
                        (place.address_components[2] &amp;&amp; place.address_components[2].short_name || '')
                        ].join(' ');
                }
                /*********************************************************************/
                /* var address contain your autocomplete address *********************/
                /* place.geometry.location.lat() &amp;&amp; place.geometry.location.lat() ****/
                /* will be used for current address latitude and longitude************/
                /*********************************************************************/
          var lat =  document.getElementById('lat').innerHTML = place.geometry.location.lat();
          var lon =  document.getElementById('lon').innerHTML = place.geometry.location.lng();
                });
          }
  
           google.maps.event.addDomListener(window, 'load', initialize);
  
            &lt;/script&gt;
  
  
  
      &lt;form method = "post"&gt;
  &lt;input id="pac-input" class="controls" type="text"
          placeholder="Enter a location"&gt;
         &lt;div id='lat'&gt;&lt;/div&gt;
          &lt;div id='lon'&gt;&lt;/div&gt;
  &lt;button type="submit" formaction="validate.php" name="submit" formmethod="post"&gt;Oddaj&lt;/button&gt;
          &lt;/form&gt;
     &lt;script type="text/javascript"&gt;
   $(document).ready(function() {
  
              $(".clickable").click(function() {
                  var lat = $(this).attr('lat');
                  //alert($(this).attr('id'));
                  $.ajax({
                      type: "POST",
                      url: 'validate.php',
                      data: {
                            lat: lat,
                                lon: lon
                           },
                      success: function(data)
                      {
                          alert("success!");
                      }
                  });
              });
          });
  &lt;/script&gt;
  </code></pre>
  
  <p>and here is the PHP responsible to talk to database</p>
  
  <pre><code>&lt;?php
  header('Content-type: text/html; charset=utf-8');
  if (isset($_POST['submit']))
  {
           $lat = $_POST['lat'];
              $lon = $_POST['lon'];
  }
  else
  {
      echo "Nisem dobil!";
  }
  
  
      $con= mysqli_connect("localhost","username","password","database");
  
  // Check connection
  if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
  
  // escape variables for security
  $lat = mysqli_real_escape_string($con, $_POST['lat']);
  $lon = mysqli_real_escape_string($con, $_POST['lon']);
  
  $sql="INSERT INTO stranka (lon, lat) VALUES (" . $lon . ", ". $lat . ")";
  
  if (!mysqli_query($con,$sql)) {
      echo "Ta error je tukaj, ker že v 5 vrstici ne dobim variable.";
      exit;
  }
  else {
      echo "Uspelo je";
  }
  
  mysqli_close($con);
  ?&gt;
  </code></pre>
  
  <p>Can someone point me to the result? </p>