I have designed a contact form in php but for the life of me can't work out how I can set it so that if the user types in the wrong details and the page refreses it keeps the data that was originally typed in. I have simply setup the contact page so that if the data typed in is not valid it will add a message to the header. When this message is added however it always wipes all the previous data. Here is the code I am using:
Code: Select all
<?php
// start PHP session
session_start();
if(isset($_POST['docontact']))
{
$to = "yes@email.com";
$def_subject = "HELP!";
$min_name_len = 2;
$min_message_len = 5;
if (
strtoupper($_POST['code']) == $_SESSION['code']
)
{
if(
isset($_POST['name']) and
strlen($_POST['name']) >= $min_name_len and
isset($_POST['message']) and
strlen($_POST['message']) >= $min_message_len and
isset($_POST['email']) and
preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $_POST['email'])
)
{
$subject = (isset($_POST['subject'])) ? $_POST['subject'] : $def_subject;
$message = $_POST['message'] ."\n==================================================\n" .$_POST['name'] ." | " .$_POST['email'];
$header = "From: " .$_POST['name'] ." <" .$_POST['email'] .">\r\n";
mail($to, $subject, $message, $headers);
header("location: ?" .$_SERVER['QUERY_STRING'] ."&sent");
}
else
{
header("location: ?" .$_SERVER['QUERY_STRING'] ."&fillall");
}
}
else
{
header("location: ?" .$_SERVER['QUERY_STRING'] ."&wrongcode");
}
}
?>Code: Select all
<?php
if(isset($_GET['sent']))
{
echo "<p class=\"success\">Thank you, your message was sent successfully.</p>";
}
if(isset($_GET['wrongcode']))
{
echo "<p class=\"wrongcode\">You have entered the wrong code. Please try again.</p>";
}
if(isset($_GET['fillall']))
{
echo "<p class=\"error\">Please fill out all mandatory fields. This error may also occur if your email address is invalid.</p>";
}
?>Many thanks!
Russ