2

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

1
  • What is "a" ? If it is the success state of the request, you should use it in a callback, because your request is asynchronous... Commented Oct 22, 2010 at 10:15

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

So all requests that came from a page are aborted when the page is left?
I believe so - I had a problem with the request being aborted on navigating away, but it also could have been because it was in an iframe. At the very least, though, it's not safe. There's a good chance you'll leave the page before the ajax request can even finish sending.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.