Page 1 of 1
Refresh page
Posted: Sat Apr 10, 2010 4:08 pm
by andrejanubis
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.
Re: Refresh page
Posted: Sat Apr 10, 2010 7:40 pm
by lunarnet76
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;
}
Re: Refresh page
Posted: Sat Apr 10, 2010 8:17 pm
by requinix
The other common solution is to redirect the user someplace.
Re: Refresh page
Posted: Sun Apr 11, 2010 3:48 am
by andrejanubis
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.
Re: Refresh page
Posted: Sun Apr 11, 2010 4:43 am
by requinix
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.
Re: Refresh page
Posted: Sun Apr 11, 2010 6:01 am
by andrejanubis
Yes tasairis, but would redirecting on different page solve my problem. I have tried but no success.
Re: Refresh page
Posted: Sun Apr 11, 2010 7:12 am
by lunarnet76
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

Re: Refresh page
Posted: Sun Apr 11, 2010 9:27 am
by andrejanubis
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.