PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
andrejanubis
Forum Newbie
Posts: 4 Joined: Sat Apr 10, 2010 3:55 pm
Post
by andrejanubis » Sat Apr 10, 2010 4:08 pm
Hello to everyone. My problem is with send an email with php. After sending an email, if user refreshes the page the email is send again. How can I prevent this? Bellow is some of the code. Thank you.
Code: Select all
<script language="JavaScript"><!--
function checkForm()
{
if (document.email.send_to.value.length != 0)
document.email.submit();
else
alert("Email is mandatory!");
}
//--></script>
</head>
<body>
<table width="500" cellspacing="0" cellpadding="2" border="0" align="center">
<tr>
<td width="120" class="text">Send to:</td>
<td class="text"><input type="text" name="send_to" style="width: 300px" class="text"></td>
</tr>
<tr>
<td width="120" valign="top" class="text"> </td>
<td class="text" align="center"> <br />
<input type="button" onClick="checkForm();" value="Send" name="send_mess">
</td>
</tr>
</table>
</form>
<?php
$send_to = $_REQUEST['send_to'];
if ( isset($send_to) )
mail( $send_to, $subject, $message, $headers )or die("Could not send e-mail");
?>
The problem is that after sending an email the value send_to is not cleared and email can be send again. How can I clear it for next session. I tied session_destroy but send_to still exists.
lunarnet76
Forum Commoner
Posts: 67 Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh
Post
by lunarnet76 » Sat Apr 10, 2010 7:40 pm
use the session!
Code: Select all
$send_to = $_REQUEST['send_to'];
if ( isset($send_to) && !isset($_SESSION['emailSent']) ){
mail( $send_to, $subject, $message, $headers )or die("Could not send e-mail");
$_SESSION['emailSent']=true;
}
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sat Apr 10, 2010 8:17 pm
The other common solution is to redirect the user someplace.
andrejanubis
Forum Newbie
Posts: 4 Joined: Sat Apr 10, 2010 3:55 pm
Post
by andrejanubis » Sun Apr 11, 2010 3:48 am
Sorry tasairis, but redirect is not an option for me, I would like to stay on same page. Thanks anyway
Sorry lunarnet76, it still does not work. As you can see from code bellow that I am posting again, now entirely,
I have a 5 second "window" in witch I let the user now where the mail was send. In this "window", if the user refresh the page the mail gets send again. How can I stop that mail to get send?
Thanks for help!
Code: Select all
<html>
<head>
<script language="JavaScript"><!--
function checkForm()
{
if (document.email.send_to.value.length != 0)
{
document.email.submit();
}
else
alert("Email is mandatory!");
}
//--></script>
</head>
<body>
<form name="email" method='request' action='mail_form.php'>
<table width="500" cellspacing="0" cellpadding="2" border="0" align="center">
<tr>
<td width="120" class="text">Send to:</td>
<td class="text"><input type="text" name="send_to" style="width: 300px; background-color: #ff9;" class="text"></td>
</tr>
<tr>
<td width="120" class="text">Subject:</td>
<td class="text"><input type="text" name="Subject" value="subj" style="width: 300px" size='40' class="text"> </td>
</tr>
<tr>
<td width="120" valign="top" class="text"> </td>
<td class="text" align="center"> <br />
<input type="button" onClick="checkForm();" value="Send message" class="text" name="send_mess">
</td>
</tr>
</table>
</form>
<?php
$send_to = $_REQUEST['send_to'];
$subject = $_REQUEST['Subject'];
$headers .= 'From: Reminder <birthday@example.com>' . "\r\n";
if ( isset($send_to) && !isset($_SESSION['emailSent']) )
{
mail( $send_to, $subject, 'test', $headers )or die("Could not send e-mail");
$_SESSION['emailSent']=true;
echo '<meta http-equiv="refresh" content="5; url=mail_form.php">';
echo "Send to <b>" . $send_to . "</b>.";
}
else
echo "Input info to send an email!";
?>
</body>
</html>
If you like, you can try it
here .
Last edited by
andrejanubis on Sun Apr 11, 2010 9:36 am, edited 1 time in total.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sun Apr 11, 2010 4:43 am
andrejanubis wrote: Sorry tasairis, but redirect is not an option for me, I would like to stay on same page.
Yeah... So you redirect to the same page they're already on. Never said you have to send them someplace different.
andrejanubis
Forum Newbie
Posts: 4 Joined: Sat Apr 10, 2010 3:55 pm
Post
by andrejanubis » Sun Apr 11, 2010 6:01 am
Yes tasairis, but would redirecting on different page solve my problem. I have tried but no success.
lunarnet76
Forum Commoner
Posts: 67 Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh
Post
by lunarnet76 » Sun Apr 11, 2010 7:12 am
My code works, and prevent any email to be sent again, but if it does not work for you it might be because the session is not started !
use
Code: Select all
if(!isset($_SESSION))session_start();
and while I'm there... does someone knows what is the best way to know if the session has started? Cause I have used this code for years but I'm still not sure
andrejanubis
Forum Newbie
Posts: 4 Joined: Sat Apr 10, 2010 3:55 pm
Post
by andrejanubis » Sun Apr 11, 2010 9:27 am
So the new code is:
Code: Select all
<html>
<head>
<?php
if(!isset($_SESSION))
session_start();
?>
<script language="JavaScript"><!--
function checkForm()
{
if (document.email.send_to.value.length != 0)
{
document.email.submit();
}
else
alert("Email is mandatory!");
}
//--></script>
</head>
<body>
<form name="email" method='request' action='mail_form.php'>
<table width="500" cellspacing="0" cellpadding="2" border="0" align="center">
<tr>
<td width="120" class="text">Send to:</td>
<td class="text"><input type="text" name="send_to" style="width: 300px; background-color: #ff9;" class="text"></td>
</tr>
<tr>
<td width="120" class="text">Subject:</td>
<td class="text"><input type="text" name="Subject" value="subj" style="width: 300px" size='40' class="text"> </td>
</tr>
<tr>
<td width="120" valign="top" class="text"> </td>
<td class="text" align="center"> <br />
<input type="button" onClick="checkForm();" value="Send message" class="text" name="send_mess">
</td>
</tr>
</table>
</form>
<?php
$send_to = $_REQUEST['send_to'];
$subject = $_REQUEST['Subject'];
$headers .= 'From: Reminder <birthday@example.com>' . "\r\n";
if ( isset($send_to) && !isset($_SESSION['emailSent']) )
{
mail( $send_to, $subject, 'test', $headers )or die("Could not send e-mail");
$_SESSION['emailSent']=true;
echo '<meta http-equiv="refresh" content="5; url=mail_form.php">';
echo "Send to <b>" . $send_to . "</b>.";
}
else
echo "Input info to send an email!";
?>
</body>
</html>
but now, no second mail can be send. I give up, thanks anyway.
What about two pages? What part should I change if I was to redirect the page after sending an email to some other page that would tell to whom it was send? If I just change the line:
Code: Select all
echo '<meta http-equiv="refresh" content="5; url=mail_form.php">';I loose the $send_to variable and I can not out put the sender.