Forum Moderators: coopster

Message Too Old, No Replies

Form Data Disappears When Clicked Back

         

neiltheblue

10:10 am on Nov 21, 2006 (gmt 0)

10+ Year Member



Hi

I have actually done a search for this here but have not been able to put an answer into practice as yet. I am creating a large online form for an intranet in work and before i go any further with all the other fields i need to make sure the form data is still there if i click the browser back button. At the moment it does not save.

I have a contact.php page which holds my form and this actions to sendmail.php ehich does my processing. An example of my contact.php is below.

<table colspan="2">
<tr>
<td>

<form action="index.php?page=scep roadshow/sendmail.php" method="post">

Name<br />
<input type="text" name="Name" value="<?php echo $_POST["Name"]?>" class="formInput" /><br />
</td>
</tr>
<tr>
<td>
E-Mail Address<br />
<input type="text" name="Email" value="<?php echo $_POST["Email"]?>" class="formInput" /><br />
</td>
</tr>
<tr>
<td>
Telephone Number<br />
<input type="text" name="Tel" value="<?php echo $_POST["Tel"]?>" class="formInput" /><br />
</td>
</tr>
<tr>
<td>
Message<br />
<textarea name="message" value="<?php echo $_POST["message"]?>" rows="6" cols="50" class="formInput"></textarea><br />
</td>
</tr>
</table>

Now here is an example of my sendmail.php page

<?php

/*------------validation---------------*/

$name = $_POST['Name'];
$email = $_POST['Email'];
$tel = $_POST['Tel'];
$message = $_POST['message'];

if (!$name ¦¦!$email ¦¦!$tel ¦¦!$message)
{
echo "<h3>Error!</h3><p class=\"error\">Please ensure all the fields are complete</p>";
$valid = "false";

}
elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
echo "<h3>Error!</h3><p class=\"error\">Please enter a valid e-mail address</p>";
$valid = "false";

}
elseif(!eregi("[0-9]", $tel))
{
echo "<h3>Error!</h3><p class=\"error\">Please enter a valid Telephone Number</p>";
$valid = "false";

}
elseif (!ereg("[A-Za-z]",$name))
{
echo "<h3>Error</h3><p class=\"error\">Please enter a valid name.</p>";
$valid = "false";
}

elseif ($valid!= "false")
{

//Save visitor name and entered message into one variable:
$formcontent="NAME:\n$name \n\nEMAIL:\n$email \n\nTEL:\n$tel \n\nMESSAGE:\n$message";
$recipient = "myemail@test.com";
$subject = "Contact Form";
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Failure!");
echo "Thank You! Your Form Was Sent Successfully";
}
?>

With these examples in practice the data does not stay in the fields when i click back. Can you help

wheelie34

10:19 am on Nov 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi

Add this to your error echo's
<a href="javascript:history.back(1)">Go Back</a>

Although some people may have javascript turned off, it will work for most.

neiltheblue

11:46 am on Nov 21, 2006 (gmt 0)

10+ Year Member



thanks m8 works great.

wheelie34

10:46 am on Nov 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



your welcome