Page 1 of 1

Double post problem

Posted: Wed May 09, 2012 7:23 am
by GKaplan
My problem is with a mail form. Everything works ok untill you refresh the internet navigator or you hit the back arrow. If you refresh it resends the mail so I receve it twice. And if you hit back all the information is still there. I've tried a huge amount of possible solutions and none worked. I'm very new to PHP and I don't know where the problem is, so here is my code just in case somebofy can help. Thanks.

Code: Select all

input {
background: #D3D3D3;
border: 1px solid #851111;
margin-right: 5px;
}
</style>

</head>

<body>

<div id="Base">

<div id="Titulo"><a href="Index.html" target="_self"><img src="Imagenes/TITULO.GIF" width="555" height="79" border="0" /></a></div>
<div id="Presentacion">

<div id="Contenido1">


<form name="consulta" id="consulta" action="?" method="post">
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" WIDTH="90%">
<TR><TD width="16%"><DIV align="right"><B>Name*:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="name" size="40"></TD></TR><TR><TD>
<TR><TD width="16%"><DIV align="right"><B>Surname*:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="apellidos" size="40"></TD></TR><TR><TD>
<TR><TD width="16%"><DIV align="right"><B>Address:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="direccion" size="40"></TD></TR><TR><TD>
<TR><TD width="16%"><DIV align="right"><B>Phone:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="telefono" size="40"></TD></TR><TR><TD>
<DIV align="right"><B>Email*:</B></DIV></TD><TD>
<INPUT type="text" name="email" size="40"></TD></TR><TR><TD>
<DIV align="right"><B>Comment*:</B></DIV></TD><TD>
<TEXTAREA name="comment" cols="50" wrap="virtual" rows="6"></TEXTAREA></TD></TR><TR><TD>&nbsp;</TD><TD>
<INPUT type="submit" name="submit" value="Submit">
<INPUT type="reset" name="reset" value="Reset"></TD></TR></TABLE></FORM>

<?php

///////Configuración/////
$mail_destinatario = 'sfthy@dfgj.com';
///////Fin configuración// 


if (isset ($_POST['submit'])) {
	
	if($_POST['name'] == "" || $_POST['apellidos'] == "" || $_POST['email'] == "" || $_POST['comment'] == ""){
		die("You must complete fielsds with *");
	}

	$headers .= "From: ".$_POST['email'];

	if ( mail ($mail_destinatario, $_POST['asunto'], "Nombre: ".$_POST['name']."\n Apellidos: ".$_POST['apellidos']."\n Dirección: ".$_POST['direccion']."\n Teléfono: ".$_POST['telefono']."\n Mensaje: ".stripcslashes ($_POST['comment']), $headers )) {
	
	echo 'Your message has been sent.';
	unset($_POST['name']);
	}
	
else echo '

Error. Please try again later.

'; 
}

?>


</div>
</div>
<div id="Menu">
  <ul id="MenuBar1" class="MenuBarHorizontal">
  <li><a href="The School.html" target="_self">Who we are</a>    </li>
  <li><a href="#" class="MenuBarItemSubmenu">Courses</a>
    <ul>
      <li><a href="Courses kids.html" target="_self">Kids and teens</a></li>
      <li><a href="Personalized.html" target="_self">Private classes</a></li>
    </ul>
  </li>
  <li><a href="#">Where we are</a></li>
  <li><a href="#">Contact</a></li>
  </ul>
</div>
<div id="Follow"><img src="Imagenes/follow us.GIF" width="70" height="16" /></div>
<div id="Facebook"><a href="https://www.facebook.com/profile.php?id=100003059865271&sk=wall" target="_blank"><img src="Imagenes/facebook.GIF" width="30" height="30" border="0" /></a></div>
<div id="En"><img src="Imagenes/Union Jack3bn.JPG" width="32" height="21" border="0" /></div>
<div id="Es"><a href="Form_es.html" target="_self"><img src="Imagenes/Espana3.JPG" width="32" height="21" border="0" /></a></div>
<div id="fondom"></div>
</div>


<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
</script>
</body>
</html>

Re: Double post problem

Posted: Wed May 09, 2012 7:40 am
by Weiry
The only effective way i can see of doing this is checking against a database entry to see if a mail was already sent before actually sending anything. The only problem here is that you have to:
A) use a database
B) store all the form's information so you can run checks against the database.

Alternatively it could be a little more complicated but storing less information, but you could set up a database with only 3 fields or so.
id | hash | mailed

where
id is an arbitrary primary key
hash is an md5 hash of the contents of $_POST (?) meaning you can check the contents of the $_POST to see if everything is exactly the same quickly.
mailed is a boolean 0|1

Not sure though on how a hash of the $_POST array might work though, ive not tried it before.

The problem with the information being stored when the back button is pressed can be solved by automatically clearing the page of any content through javascript. Unfortunately i don't know of a cleaner way to do it other than issuing a javascript form reset function on page load unless you try to use php to do it (which im not sure php will work given that its the browser auto filling the content anyways).

Re: Double post problem

Posted: Wed May 09, 2012 7:57 am
by robnet
You could try issuing a header redirect on successful sending.

Code: Select all

header("Location: " . $SuccessURL );
http://www.php.net/manual/en/function.header.php

This means the actual sendmail is done in an intermediate page that the user can't _easily_ go back to. Likewise, a page reload will not re execute the sendmail page but the one you redirected to.

Re: Double post problem

Posted: Wed May 09, 2012 8:23 am
by GKaplan
I've tried with header line but I get a error message with: headers already sent...

Re: Double post problem

Posted: Wed May 09, 2012 8:29 am
by robnet
Ahh yes, make sure no output is sent before your header is sent. In your case, probably just need to move your email sending code right to the top of the page before the first piece of html.

Re: Double post problem

Posted: Wed May 09, 2012 8:41 am
by GKaplan
Thanks robnet but that didn't work neither. I didn't get the error messages but it didn't redirect to another page and I got two email anyway!!

Re: Double post problem

Posted: Wed May 09, 2012 8:47 am
by Celauran
GKaplan wrote:Thanks robnet but that didn't work neither. I didn't get the error messages but it didn't redirect to another page and I got two email anyway!!
Then please post your updated code so we can take a look.

Re: Double post problem

Posted: Wed May 09, 2012 9:18 am
by GKaplan
Ok thank you. Actually now when I go to the page the wait 5 minutes message comes out in a completly white screen. Here is my code

Code: Select all

<?php

session_start();

if(isset($_SESSION['timeout']) && $_SESSION['timeout'] < strtotime("now")){
      echo "You cannot submit another e-mail so soon, please wait a few minutes";
      exit();
}


///////Configuración/////
$mail_destinatario = 'xcgfh@cdfgh.com';
///////Fin configuración// 


if (isset ($_POST['submit'])) {
	
	if($_POST['name'] == "" || $_POST['apellidos'] == "" || $_POST['email'] == "" || $_POST['comment'] == ""){
		die("You must complete fielsds with *");
	}

	$headers .= "From: ".$_POST['email'];

	if ( mail ($mail_destinatario, $_POST['asunto'], "Nombre: ".$_POST['name']."\n Apellidos: ".$_POST['apellidos']."\n Dirección: ".$_POST['direccion']."\n Teléfono: ".$_POST['telefono']."\n Mensaje: ".stripcslashes ($_POST['comment']), $headers )) {
	
	echo 'Your message has been sent.';
	$_SESSION['timeout'] = strtotime("+5 minutes");
	}
	
else echo '

Error. Please try again later.

'; 
}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
#Base {
	position:absolute;
	left:50%;
	top:32px;
	width:900px;
	height:600px;
	z-index:1;
	margin-left: -450px;
}
#Titulo {
	position:absolute;
	left:22px;
	top:19px;
	width:555px;
	height:79px;
	z-index:2;
}
#Presentacion {
	position:absolute;
	left:22px;
	top:162px;
	width:838px;
	height:378px;
	z-index:3;
	border: 5px solid #696767;
}
body {
	background-color: #000000;
}
#Menu {
	position:absolute;
	left:22px;
	top:117px;
	width:722px;
	height:40px;
	z-index:8;
}
textarea {
background: #D3D3D3;
border: 1px solid #851111;
}
</style>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	border: 2px;
#ccc; 	color: #ffffff;
}
#fondom {
	position:absolute;
	left:22px;
	top:112px;
	width:844px;
	height:40px;
	z-index:2;
	background-color: #CCCCCC;
	border: 2px solid #696767;
}
#Follow {
	position:absolute;
	left:18px;
	top:567px;
	width:70px;
	height:16px;
	z-index:2;
}
#Facebook {
	position:absolute;
	left:94px;
	top:555px;
	width:30px;
	height:30px;
	z-index:2;
}
#English {
	position:absolute;
	left:607px;
	top:134px;
	width:70px;
	height:18px;
	z-index:2;
}
#Castellano {
	position:absolute;
	left:741px;
	top:134px;
	width:90px;
	height:20px;
	z-index:3;
}
#En {
	position:absolute;
	left:790px;
	top:122px;
	width:32px;
	height:21px;
	z-index:3;
}
#Es {
	position:absolute;
	left:832px;
	top:122px;
	width:32px;
	height:21px;
	z-index:3;
}
#Contenido1 {
	position:absolute;
	left:53px;
	top:33px;
	width:427px;
	height:298px;
	z-index:2;
	text-align: justify;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	line-height: inherit;
}
#Base #Presentacion #Contenido1 h1 {
	color: #851111;
}
#FOTO1 {
	position:absolute;
	left:330px;
	top:14px;
	width:501px;
	height:353px;
	z-index:2;
}
#Base #Presentacion #Contenido1 p {
	font-size: 12px;
	font-weight: bold;
	text-align: left;
}
#Presentacion-borde {
	position:absolute;
	left:22px;
	top:162px;
	width:848px;
	height:378px;
	z-index:2;
}
input {
background: #D3D3D3;
border: 1px solid #851111;
margin-right: 5px;
}
</style>

</head>

<body>

<div id="Base">

<div id="Titulo"><a href="Index.html" target="_self"><img src="Imagenes/TITULO.GIF" width="555" height="79" border="0" /></a></div>
<div id="Presentacion">

<div id="Contenido1">


<form name="consulta" id="consulta" action="?" method="post"> 
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" WIDTH="90%">
<TR><TD width="16%"><DIV align="right"><B>Nombre*:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="name" size="40"></TD></TR><TR><TD>
<TR><TD width="16%"><DIV align="right"><B>Apellidos*:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="apellidos" size="40"></TD></TR><TR><TD>
<TR><TD width="16%"><DIV align="right"><B>Dirección:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="direccion" size="40"></TD></TR><TR><TD>
<TR><TD width="16%"><DIV align="right"><B>Teléfono:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="telefono" size="40"></TD></TR><TR><TD>
<DIV align="right"><B>Email*:</B></DIV></TD><TD>
<INPUT type="text" name="email" size="40"></TD></TR><TR><TD>
<DIV align="right"><B>Consulta*:</B></DIV></TD><TD>
<TEXTAREA name="comment" cols="50" wrap="virtual" rows="6"></TEXTAREA></TD></TR><TR><TD>&nbsp;</TD><TD>
<INPUT type="submit" name="submit" value="Submit">
<INPUT type="reset" name="reset" value="Reset"></TD></TR></TABLE></FORM>


</div>
</div>
<div id="Menu">
  <ul id="MenuBar1" class="MenuBarHorizontal">
  <li><a href="The School.html" target="_self">Who we are</a>    </li>
  <li><a href="#" class="MenuBarItemSubmenu">Courses</a>
    <ul>
      <li><a href="Courses kids.html" target="_self">Kids and teens</a></li>
      <li><a href="Personalized.html" target="_self">Private classes</a></li>
    </ul>
  </li>
  <li><a href="#">Where we are</a></li>
  <li><a href="#">Contact</a></li>
  </ul>
</div>
<div id="Follow"><img src="Imagenes/follow us.GIF" width="70" height="16" /></div>
<div id="Facebook"><a href="https://www.facebook.com/profile.php?id=100003059865271&sk=wall" target="_blank"><img src="Imagenes/facebook.GIF" width="30" height="30" border="0" /></a></div>
<div id="En"><img src="Imagenes/Union Jack3bn.JPG" width="32" height="21" border="0" /></div>
<div id="Es"><a href="Form_es.html" target="_self"><img src="Imagenes/Espana3.JPG" width="32" height="21" border="0" /></a></div>
<div id="fondom"></div>
</div>


<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
</script>
</body>
</html>

Re: Double post problem

Posted: Wed May 09, 2012 9:26 am
by GKaplan
Sorry but I'm looking in several places and I've confused solutions. Now I've tried putin the headers line with the php code at the begining and it doesn't work and I'v e got this error message:

Warning: Cannot modify header information - headers already sent by (output started at /mnt/webf/b0/75/51979575/htdocs/Inicio/Form.php:18) in /mnt/webf/b0/75/51979575/htdocs/Inicio/Form.php on line 19

my code is:

Code: Select all

<?php

///////Configuración/////
$mail_destinatario = 'sdfg@adfg.com';
///////Fin configuración// 


if (isset ($_POST['submit'])) {
	
	if($_POST['name'] == "" || $_POST['apellidos'] == "" || $_POST['email'] == "" || $_POST['comment'] == ""){
		die("You must complete fielsds with *");
	}

	$headers .= "From: ".$_POST['email'];

	if ( mail ($mail_destinatario, $_POST['asunto'], "Nombre: ".$_POST['name']."\n Apellidos: ".$_POST['apellidos']."\n Dirección: ".$_POST['direccion']."\n Teléfono: ".$_POST['telefono']."\n Mensaje: ".stripcslashes ($_POST['comment']), $headers )) {
	
	echo 'Your message has been sent.';
	header("Location: http://www.page.com" );
	}
	
else echo '

Error. Please try again later.

'; 
}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact. Academia Buckingham -- English School in Ibiza</title>
<style type="text/css">
#Base {
	position:absolute;
	left:50%;
	top:32px;
	width:900px;
	height:600px;
	z-index:1;
	margin-left: -450px;
}
#Titulo {
	position:absolute;
	left:22px;
	top:19px;
	width:555px;
	height:79px;
	z-index:2;
}
#Presentacion {
	position:absolute;
	left:22px;
	top:162px;
	width:838px;
	height:378px;
	z-index:3;
	border: 5px solid #696767;
}
body {
	background-color: #000000;
}
#Menu {
	position:absolute;
	left:22px;
	top:117px;
	width:722px;
	height:40px;
	z-index:8;
}
textarea {
background: #D3D3D3;
border: 1px solid #851111;
}
</style>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	border: 2px;
#ccc; 	color: #ffffff;
}
#fondom {
	position:absolute;
	left:22px;
	top:112px;
	width:844px;
	height:40px;
	z-index:2;
	background-color: #CCCCCC;
	border: 2px solid #696767;
}
#Follow {
	position:absolute;
	left:18px;
	top:567px;
	width:70px;
	height:16px;
	z-index:2;
}
#Facebook {
	position:absolute;
	left:94px;
	top:555px;
	width:30px;
	height:30px;
	z-index:2;
}
#English {
	position:absolute;
	left:607px;
	top:134px;
	width:70px;
	height:18px;
	z-index:2;
}
#Castellano {
	position:absolute;
	left:741px;
	top:134px;
	width:90px;
	height:20px;
	z-index:3;
}
#En {
	position:absolute;
	left:790px;
	top:122px;
	width:32px;
	height:21px;
	z-index:3;
}
#Es {
	position:absolute;
	left:832px;
	top:122px;
	width:32px;
	height:21px;
	z-index:3;
}
#Contenido1 {
	position:absolute;
	left:53px;
	top:33px;
	width:427px;
	height:298px;
	z-index:2;
	text-align: justify;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	line-height: inherit;
}
#Base #Presentacion #Contenido1 h1 {
	color: #851111;
}
#FOTO1 {
	position:absolute;
	left:330px;
	top:14px;
	width:501px;
	height:353px;
	z-index:2;
}
#Base #Presentacion #Contenido1 p {
	font-size: 12px;
	font-weight: bold;
	text-align: left;
}
#Presentacion-borde {
	position:absolute;
	left:22px;
	top:162px;
	width:848px;
	height:378px;
	z-index:2;
}
input {
background: #D3D3D3;
border: 1px solid #851111;
margin-right: 5px;
}
</style>

</head>

<body>

<div id="Base">

<div id="Titulo"><a href="Index.html" target="_self"><img src="Imagenes/TITULO.GIF" width="555" height="79" border="0" /></a></div>
<div id="Presentacion">

<div id="Contenido1">


<form name="consulta" id="consulta" action="?" method="post"> 
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" WIDTH="90%">
<TR><TD width="16%"><DIV align="right"><B>Nombre*:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="name" size="40"></TD></TR><TR><TD>
<TR><TD width="16%"><DIV align="right"><B>Apellidos*:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="apellidos" size="40"></TD></TR><TR><TD>
<TR><TD width="16%"><DIV align="right"><B>Dirección:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="direccion" size="40"></TD></TR><TR><TD>
<TR><TD width="16%"><DIV align="right"><B>Teléfono:</B></DIV></TD><TD width="84%">
<INPUT type="text" name="telefono" size="40"></TD></TR><TR><TD>
<DIV align="right"><B>Email*:</B></DIV></TD><TD>
<INPUT type="text" name="email" size="40"></TD></TR><TR><TD>
<DIV align="right"><B>Consulta*:</B></DIV></TD><TD>
<TEXTAREA name="comment" cols="50" wrap="virtual" rows="6"></TEXTAREA></TD></TR><TR><TD>&nbsp;</TD><TD>
<INPUT type="submit" name="submit" value="Submit">
<INPUT type="reset" name="reset" value="Reset"></TD></TR></TABLE></FORM>


</div>
</div>
<div id="Menu">
  <ul id="MenuBar1" class="MenuBarHorizontal">
  <li><a href="The School.html" target="_self">Who we are</a>    </li>
  <li><a href="#" class="MenuBarItemSubmenu">Courses</a>
    <ul>
      <li><a href="Courses kids.html" target="_self">Kids and teens</a></li>
      <li><a href="Personalized.html" target="_self">Private classes</a></li>
    </ul>
  </li>
  <li><a href="#">Where we are</a></li>
  <li><a href="#">Contact</a></li>
  </ul>
</div>
<div id="Follow"><img src="Imagenes/follow us.GIF" width="70" height="16" /></div>
<div id="Facebook"><a href="https://www.facebook.com/profile.php?id=100003059865271&sk=wall" target="_blank"><img src="Imagenes/facebook.GIF" width="30" height="30" border="0" /></a></div>
<div id="En"><img src="Imagenes/Union Jack3bn.JPG" width="32" height="21" border="0" /></div>
<div id="Es"><a href="Form_es.html" target="_self"><img src="Imagenes/Espana3.JPG" width="32" height="21" border="0" /></a></div>
<div id="fondom"></div>
</div>


<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
</script>
</body>
</html>

Re: Double post problem

Posted: Wed May 09, 2012 9:31 am
by Celauran
You're sending your headers on line 18 with the echo statement, then trying to send them again on line 19. Display the success message on the next page instead.

Re: Double post problem

Posted: Wed May 09, 2012 9:40 am
by GKaplan
Right, thank you. It redirects to main page now. But I have another problem because when I submit without filling all the required (*) fields it displays the message "You must complete fielsds with *" in a totally white page with anything else, wich is not very usefull.

Re: Double post problem

Posted: Wed May 09, 2012 9:41 am
by Celauran
GKaplan wrote:Right, thank you. It redirects to main page now. But I have another problem because when I submit without filling all the required (*) fields it displays the message "You must complete fielsds with *" in a totally white page with anything else, wich is not very usefull.
That's how you coded it. die() is fine for debugging, but worthless in a production environment. Why not write the error to a variable then, when displaying the page, check for the existence of that variable?

Re: Double post problem

Posted: Wed May 09, 2012 10:11 am
by GKaplan
OK thank you everybody I think I got it!!