Been searching all day, found items similar but completely stuck...
I want to pass a Google Maps Autocomplete Result (latlng) into a PHP Form along with PHP variables through one submit (not two buttons, i.e. to put JS variable in hidden field then submit form via submit button)
Code at present:
<?php
if (isset($_POST['submit'])) {
// echo vars if they are working...
echo "passed data <br>";
echo $_POST['first_name'] . "<br>";
echo $_POST['geocode'] . "<br>";
// Get variables, check data and store into DB...
}
?>
Then:
<script type="text/javascript">
var geocoder;
function initialize() {
// Search options
var searchOptions = {
componentRestrictions : {country: 'nz'},
types: ['geocode']
};
var input = document.getElementById('pac-input');
var autocomplete = new google.maps.places.Autocomplete(input, searchOptions);
geocoder = new google.maps.Geocoder();
}
function codeAddress() {
var address = document.getElementById('pac-input').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById('geocode').value = (results[0].geometry.location);
} else {
//alert('Geocode was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
function submitthis() {
codeAddress();
}
</script>
<script>
$(function(){ // Document Ready
});
</script>
Finally:
<form id="myForm" name="myForm" action="google-text-input.php" method="post" >
First Name<input name="first_name" type="text"><br>
Address<input id="pac-input" class="controls" type="text" placeholder="Search Box"><br>
<input id="geocode" name="geocode" type="text" value="null"><br> <!-- to be hidden -->
<input name="submit" type="submit" value="Submit" onclick="submitthis()"><br>
</form>
I'd preferably like when I hit the Submit button it stores Google Maps result into hidden, submits form to same page then I can pull all php vars and store. I'm open to an approach which upon submit puts the javascript part into the url where I can pull PHP vars via POST and just GET the Google Result in the URL.
The only thing I can see which isn't working in the above is the form gets submitted before my codeAddress() has completed. As if I put return false to prevent form submit it updates my hidden field.
Thanks in advance!