I want to submit a form once I've gotten requested geocoding results from user-side geocoding call to the google api:
var geocoder;
geocoder = new google.maps.Geocoder();
function codeAddress(address, form) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert(results[0].geometry.location);
document.getElementById("latitude").value = location[0];
document.getElementById("longitude").value = location[1];
return validate2(form);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
EDIT: as per request here is the code for the other 2 functions:
<form method='post' action='profile.php' onSubmit='return validate(this)' name='signup'>
onSubmit calls validate(form) which calls the geocode function:
function validate(form)
{
return codeAddress(form.streetNumN.value +" " +form.streetN.value+" "+form.streetTypeN.value+", "+form.cityN.value +", "+form.stateN.value+" "+form.zipN.Value, form);
}
function validate2(form)
{var fail = !form.firstNameN.value + !form.lastNameN.value + !form.phoneN.value + !form.emailN.value + !form.passN.value + !form.pass2N.value + !form.emailN.value + !form.streetNumN.value + !form.streetN.value + !form.streetTypeN.value + !form.cityN.value + !form.stateN.value + !form.zipN.value;
if(form.firstNameN.value.length == 0)
{
document.getElementById("firstNameP").innerHTML="Please enter your first name.";
}
if(form.lastNameN.value.length == 0)
{
document.getElementById("lastNameP").innerHTML="Please enter your last name.";
}
...etc...
if(fail>0)
return false;
else
return true;
}
validate2(form) makes sure all fields are filled and then returns true if form is filled properly or false otherwise.
Using validate(form) to simply return validate2(form) works fine. But using validate(form) to return geocode(...) which is suppose to call validate2(form) in its completion tasks has problems....the form just submits without waiting for the geocode or for validate2(form) to return.
What does stop form submission is to have the codeAddress function return false
var geocoder;
geocoder = new google.maps.Geocoder();
function codeAddress(address, form) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert(results[0].geometry.location);
alert('hi from geocode completion');
document.getElementById("latitude").value = results[0].geometry.location[0];
document.getElementById("longitude").value = results[0].geometry.location[1];
//return false;
return validate2(form);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
return false;
}
in the main body apart from the completion handler. So I think this function must be returning before the completion handler runs, hence form is submitted. How can I stop this function from returning? (I've also tried just running the functions rather than writing them as returns. So that only the validate2(form) function returns at the end. In this case everything has time to run but the form submits regardless of what validate2(form) actually returns.
What I am trying to do is done often. Not sure why I can't find a template, if anyone has some sample code or recommendations to find sample code, that would be much appreciated.