jQuery load() before form submit
On my form submit, I'm trying to use load() to run a PHP script, add data to my database, then submit the form on completion. However, if the form submits true, the data isn't added to my database, but if it returns false, it is. Here's basically what I'm looking at:
if(//form not empty){
$j('#allset').load(url, function(){
return true;
});
} else {
return false;
开发者_如何学C}
I don't know if it matters, but the form I'm trying to submit is a paypal form.
Anyone know what I can do to fix this? Thanks!
Returning true
from the callback will have no effect. The event handler will return immediately after it made the Ajax call. It will not wait for the call to finish (Ajax is asynchronous).
You have prevent form submission in any case and then submit the form again, once the Ajax request is completed. Something like this:
(function() {
var processed = false;
$(form).submit(function(event){
if(processed) { // data sent fo the server?
processed = false;
return true;
}
event.preventDefault(); // prevent form submission
if(/* form not empty */) {
$.get(url, function() { // do you need load?
processed = true;
$(form).submit();
});
}
});
}());
精彩评论