Thomas John
Thomas John

Reputation: 1899

simple ajax question

I have to send mail before submitting my php page, I am submitting the page using javascript. my mail script is in sendmails.php file. so can I send an ajax request to send mail before submitting the page using java script ? like follows

function submit_page()
{
//trying to run send_mail.php
..............................//ajax codes
............................
xmlhttp.open("GET","send_mail.php",true);
xmlhttp.send();
.................................

if(a)
form.action = 'one.php'
else
form.action = 'two.php'
form.submit()//form submitting using javascript
}

Will it run the send_mail.php file in the server ?

Thank you

Upvotes: 2

Views: 81

Answers (2)

Sam Dufel
Sam Dufel

Reputation: 17608

I doubt it - ajax requests take a certain amount of time to complete, and you're navigating away from the page immediately, which will interfere with the request.

Suggestion - do the ajax request in synchronous mode. That way you're guaranteed it will finish before the form submits.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328830

Yes but don't rely on the fact that this happens before send() returns. send() just starts a background thread which will eventually open the connection to send_mail.php and post the form. So it can happen that the form is submitted before the mail is sent.

Upvotes: 2

Related Questions