tenants
Well-known member
Sounds simple right?
What I want to be able to do is send the ajax request, then once it has returned, send the normal form submission (which redirects)
The form submission is core functionality, so it has to form submit and redirect (the ajax on submit is something I am adding)
so...
I thought about doing this by first stopping the normal redirect on submit ( e.preventDefault() ) , doing the ajaxy thing, then getting the ajax to re-submit the form
But the problem is, if you try to submit (clicking a submit button) the form with doAjaxThing, then you'll hit the e.preventDefault();
- thats not good, since I want the form to redirect to "endUpHere" after the ajax has been sent...
so..
What I then did was to set a few parameters, and unbind this e.preventDefault() if we are sending from doAjaxThing
Now this is starting to feel very hacky, and while it works in most broswers, I've seen issues in IE
So... I'm disgusted with this code, it's yucky and doesn't seem to work in all browsers (IE wants to submit twice it seems).
Am I thinking about this all the wrong way (I'm not really a front end thinker, I'm more PHP backend, so the script is giving me a bit of grief
What I want to be able to do is send the ajax request, then once it has returned, send the normal form submission (which redirects)
The form submission is core functionality, so it has to form submit and redirect (the ajax on submit is something I am adding)
so...
I thought about doing this by first stopping the normal redirect on submit ( e.preventDefault() ) , doing the ajaxy thing, then getting the ajax to re-submit the form
Code:
<form action="endUpHere" method="post" class="xenForm AutoValidator">
<stuff> </stuff>
</form>
....
$('.xenForm').submit(function(e) {
e.preventDefault();
doAjaxThing();
}
But the problem is, if you try to submit (clicking a submit button) the form with doAjaxThing, then you'll hit the e.preventDefault();
- thats not good, since I want the form to redirect to "endUpHere" after the ajax has been sent...
so..
What I then did was to set a few parameters, and unbind this e.preventDefault() if we are sending from doAjaxThing
Code:
$('.xenForm').submit(function(e) {
if(!hasAjaxed)
{
isGlobal = true;
e.preventDefault();
doAjaxThing();
}
else
{
$(this).unbind('submit').submit();
isGlobal = true;
return true;
}
}
Code:
doAjaxThing(){
var xenFormOb = $('.xenForm');
$.ajax({
type: "POST",
url: "stuff/forajax",
data: dataString,
global: isGlobal,
success: function(){}}).done(function(){hasAjaxed = 1; xenFormOb.submit();})
});
}
Now this is starting to feel very hacky, and while it works in most broswers, I've seen issues in IE
So... I'm disgusted with this code, it's yucky and doesn't seem to work in all browsers (IE wants to submit twice it seems).
Am I thinking about this all the wrong way (I'm not really a front end thinker, I'm more PHP backend, so the script is giving me a bit of grief
Last edited: