I’m using a PHP file (as part of a wordpress plugin), which contains several functions.
One of them builds an html form and another one deals with the submitted form.
I’ve added to the function that builds the form a submit() function in which I call an asynchronous function.
In order for it to finish before the form is submitted I’m using preventDefault() and a call to submit() when the async function finishes.
The problem is that my call to submit() after preventDefault() causes different results in the PHP function that handles the submitted form than the original submit.
Here’s the PHP code:
if(isset($_POST['submit-reg']))
{
$output = sreg_process_form($atts);
return $output;
}
else
{
$data = array();
$form = simplr_build_form($data, $atts);
return $form;
}
When I don’t use preventDefault() and submit() the condition is positive and the form is submitted normally.
When I do use them the second part is executed and the form is being rebuilt instead of submitted.
So the question is: what in preventDefault() and then submit() causes isset($_POST['submit-reg'])) to be false and how can I fix it?
edit: Here’s the JS:
$('#simplr-reg').submit(function(event)
{
event.preventDefault();
codeAddress(function(success){}); //callback from googlemaps
});
function codeAddress(callback)
{
var geocoder = new google.maps.Geocoder();
geoCodingStarted = true;
geocoder.geocode( { 'address': address}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
$("input[name=latitude]").val(latitude);
$("input[name=longitude]").val(longitude);
$('#simplr-reg').submit();
}
});
} // end of code address
When you submit the form via normal methods then
submit-regis set. When you use preventDefaul() and send the form asynchronously then the value is never set, because the form is never submited, altough it’s processed asynchronously. You should use another method to check is the form is being processed or built. Perhaps a hidden field. or another flag set dinamicaly on the page.From the W3C
When you use
preventDefault()you prevent this from happening making the variable ´submit-reg` unexistant because it is never set.More info from jQuery site