Page 1 of 1
Send mail problem
Posted: Wed Apr 02, 2008 2:11 am
by frijolita
Hi all,
I've got a php page with a form.
When i click submit, i call another php which is not mine so i cannot modify it.
The thing is that i would like before calling this action, to send an email containing form selected values.
I know how to send emails in php, but my doubt is how to call this mail function when i click submit button and before calling the appropiate action
Thanks!
Re: Send mail problem
Posted: Wed Apr 02, 2008 5:29 am
by mchaggis
Hi,
There are 3 solutions as I see it....
1... The cheats method
Create a second php page with the exact same form, that mails the submitted data, then displays the form pre filled with the values and onload the form is submitted automatically:
Code: Select all
<?php
/**
* Code to mail contents of $_POST array goes here
*/
?>
<html>
<body onload="document.getElementById('myForm').submit()">
<p>Please waist while we process your data...</p>
<form id="myForm">
<input type="hidden" name="field1" value="<?=$_POST['field1']?>" />
<input type="hidden" name="field2" value="<?=$_POST['field2']?>" />
<input type="hidden" name="field3" value="<?=$_POST['field3']?>" />
<input type="submit" value="Click here if you are not automatically redirected" />
</form>
</body>
</html>
2... The AJAX method
Put some javascript into the form so that when the form is submited, the form is submitted to your new mail script and then allows the browser to submit the form in the normal way.
3... The pure PHP and CURL road
This is the more complexed coding route. Youo submit to a new script which contains your mail routine. Then, you can use curl to simulate a browser posting to the script that you can't modify.
http://www.php.net/curl
Re: Send mail problem
Posted: Wed Apr 02, 2008 7:31 am
by frijolita
Thanks a lot !
I've used the Ajax method so now from my javascript before doing submit i call a php function using Sajax.php
The mail is sent ALMOST correctly and the form submitted.
Now my problem is that if I don't put an alert after the javascript function which invokes the php which sends the mail, the mail is not sent ....
LIKE THIS IT WORKS:
var pepe = pasarValores();
alert ("Notification mail has been sent");
form.submit();
return(true);
LIKE THIS IT DOES NOT WORK
var pepe = pasarValores();
form.submit();
return(true);
Any idea?? thanks!!
Re: Send mail problem
Posted: Wed Apr 02, 2008 7:56 am
by mchaggis
With out seeing your ajax code I can't be sure, but ajax will call things in the background allowing any further js to be executed. So what you need to do is wait for the correct response to come back from the ajax call prior to executing, instead of just executing the form submit...
Code: Select all
http = = new XMLHttpRequest();
function parseValores() {
http.open('post', '/login/wsLogin.php/login');
http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http.onreadystatechange = LoginResponse;
http.send(dataToSend);
}
function LoginResponse() {
if (http.readyState == 4) {
form.submit();
}
}
The readyState 4 denotes that the ajax call has finished.