post form with ajax and then submit same form to other url?
Hi,
I'm working with a 3rd party API which does not support ajax so I need to submit my form to them the normal html/php way. But before doing this, I need to use ajax to post the form data to another php script which does some processing and sends and email.
I thought returning true would work since typically for javascript validation returning false keeps it from submitting twice but that's not working.
I did get it to trigger submit with
Code:
$('form').submit();
But that throws it into a loop.
I need to be able to add a class and then remove it I think to make it only run the javascript on the first run but removing the class doesn't seem to work for one that's hard coded in the html form. I tried adding 'contact' class and then do a if hasClass check before the ajax post but even after removing the class on success, it still is in a loop. You can see in the following code why it is still in a loop but maybe suggestions along the same line? A way to see if the form has already been submitted via ajax so it will ignore the ajax and submit normally?
Code:
$(document).ready(function(){
$('form').submit(function(e){
e.preventDefault();
$('form').addClass('contact');
if($('form').hasClass('contact')){
var fname = $('input#fname').val();
var dataString = "fname=" + fname;
$.ajax({
type: "POST",
url: "test1.php",
data: dataString,
success: function() {
//$("#contact").submit();
console.log("ajax success");
$('form').removeClass('contact');//remove the class
$('form').submit();
}
});
}
});
});
nope, tried this.submit() too and it's not a function.
As I said, $('form').submit() is working but because it's inside a $('form').submit function it keeps looping submitting with ajax every time.
I'm trying to work out the logic to add a hidden field on the first go around and then skip the whole jquery block and submit normally as if jquery wasn't there. I think I just figured it out in my head. I'll post back here if it works.
It's been working the whole time. Before I started trying to figure out how to double submit this way, it was working but I couldn't tell just by having the php script echo out a message.
I replaced the success message in test1.php with a simple email function and it's working just fine.
Ajax posts to the test1 email script which is what I wanted and then the form submits and redirects like a normal php form to test2. I checked it several times and even though it looks like it's just skipping straight to test2, it's sending the email from test1.
Bookmarks