By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,319 Members | 2,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,319 IT Pros & Developers. It's quick & easy.

mail() function sending twice

LacrosseB0ss
100+
P: 113
Hey all!

I am coding a page in php to send an e-mail to 2 addresses (to and cc). The user fills out 2 text boxes on a form. The first will be the body of the message and the 2nd is return address. Everything is working however when the body text box is clicked, it sends a blank e-mail. Then when "submit" is clicked the real one is submitted.

Does anyone know how to prevent the first e-mail from sending? Or has anyone else encountered this problem? Thanks!

- LB
Oct 31 '07 #1
Share this Question
Share on Google+
8 Replies


Atli
Expert 5K+
P: 5,058
Hi LB. Welcome to TSDN!

Thats a weird problem. Could you show us the HTML markup for the body text box?
Nov 1 '07 #2

brettl
P: 41
Yeah that sounds really strange... I would love to see the code you are using.
Nov 1 '07 #3

LacrosseB0ss
100+
P: 113
code is as follows:
HTML
Expand|Select|Wrap|Line Numbers
  1. <form action="../Prayer.php" method="post" name="frmPrayer" id="frmPrayer">
  2.       <div align="center"><strong>Enter your request in the box below and hit "send" to submit:</strong></div>
  3.     <center>
  4.         <textarea name="txtRequest" id="txtRequest" cols="100" rows="3"></textarea>
  5.         <br />
  6.       <br />
  7.       <div align="center"><strong>If you would like a reply, enter your e-mail or leave blank to remain anonymous:</strong></div>
  8.       <input name="txtReply" id="txtReply"  type="text" size="60">
  9.       <br />
  10.       <br />
  11.       <input name="cmdSendReq" type="submit" value="Submit Request" onClick="<?php echo(sendMail()) ?>">
  12.     </center>
  13. </form>
  14.  
and php:
[php]
<?php
function sendMail()
{
$to = "user@example.com";
$subject = "New Online Prayer Request";
$message = $_POST['txtRequest'];
$txtReply = $_POST['txtReply'];

$headers = "To: Bloor Central Prayer" . "\r\n";

if($txtReply == "") {
$headers .= "From: user@example.com" . "\r\n";
} else {
$headers .= "From: ";
$headers .= $_POST['txtReply'] . "\r\n";
}

$headers .= "Cc: webmaster@example.com";
/*temp test - check all e-mails make it, spam*/

$sent = mail($to, $subject, $message, $headers);

if($sent)
{
echo "Message Sent Successfully";
} else {
echo "There Was an Error Sending Your Message";
}
}
?>
[/php]

Thanks all!
Nov 2 '07 #4

LacrosseB0ss
100+
P: 113
I'll add, as you can see by the comment, the CC e-mail is only a temporary thing while we get the site off the ground. I just want to ensure everything is going to the right places and that we're spam free and so on and so forth. I highly doubt this is where the problem is but I'm adding the comment anyway.

Also, the first e-mail is sent when EITHER text box gets focus.

-LB
Nov 2 '07 #5

Atli
Expert 5K+
P: 5,058
Hi.

The problem is not with your textboxes. The problem is with the submit button's onClick event:
Expand|Select|Wrap|Line Numbers
  1. onClick="<?php echo(sendMail()) ?>"
  2.  
PHP functions can not be called from client-side code. PHP being a server side language, the PHP code in that line would be executed on the server before the form even reached the client.

The result of the function would be replaced in the form the client receives and a blank email sent. I'm sure, if you check out the source in your browser, that this line actually looks something like this:
Expand|Select|Wrap|Line Numbers
  1. onClick="Message Sent Successfully"
  2.  
Try removing the onClick event all together, it should solve your problem.
Nov 2 '07 #6

LacrosseB0ss
100+
P: 113
I understand now. I have never used PHP before and your explanation makes sense. And you were bang on with the onClick prediction I just didn't know why it was doing that.

I followed your advice and it now doesn't send out multiple e-mails. In fact, it doesn't send any when the button is clicked. The sendMail() function isn't being called anymore. I'm lost again.

Is there an onSubmit() event that could work better? I'll continue to play with it. Thanks for the advice!

- LB
Nov 5 '07 #7

Atli
Expert 5K+
P: 5,058
Hi.

Your mail is not getting sent at all now because your sendMail() function is never getting called. Sorry, should have caught this earlier.

The onClick event you removed earlier was not only responsible for sending the empty email, but the other one as well. Your form is posting its data to the same page it is on, effectively refreshing the page and sending along the form data.

Because your onClick event contained the sendMail() function, it was being sent ACCIDENTALLY in the wrong place.

Try either adding a call to the sendMail() function if the data is sent, or extracting the code from the function, calling it if the data has been submitted.

For example, try adding this after your sendMail() function:
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['cmdSendReq'])) {
  2.   sendMail();
  3. }
  4.  
Nov 7 '07 #8

LacrosseB0ss
100+
P: 113
For example, try adding this after your sendMail() function:
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['cmdSendReq'])) {
  2.   sendMail();
  3. }
  4.  
THANKS! Works like a charm!! The question now is, why? What does the "isset" function?

Thanks again for the help. It has cleared up the problem.
- LB
Nov 8 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.