Hello all,

This pertains to a form I have on my page.
As it is now, if the person filling out the form makes a mistake or leaves something out, the form reloads, but all the prior responses are wiped out.
I can see how this might annoy someone, to have to fill everything out all over again, even if they neglected to fill in just one field.
What I would like is to have the error noted (as it is now) but to reload the values the visitor posted before hitting "Submit".
I assume the way to do this is via session cookies (the kind that only last while the browser is open).
I must have looked at about 50 different sites on the net, but I could not find a simple explanation. Most go off on some complex application of setting cookies, far beyond what I am looking for.

Basically, all I am looking for is:

A) A way to set the above cookie

B) Where to place it
it would be nice to have:

C) The cookie is destroyed upon SUCCESSFUL completion of the form.

Can anyone please help?

Below is my code:


 $RandomStr = md5(microtime());
 $Str = substr($RandomStr,0,5);
if($_GET['error'] == "empty") 
$msg = "Please fill all form fields.";
 elseif($_GET['error'] == "name") 
 $msg = "Please fill in your name.";
 elseif($_GET['error'] == "email") 
 $msg = "Please provide valid email address.";
 elseif($_GET['error'] == "phone") 
 $msg = "Please provide your daytime phone number.";
 elseif($_GET['error'] == "event_title") 
 $msg = "Please provide the title of the event.";
 elseif($_GET['error'] == "event_date_and_time") 
 $msg = "Please provide the date and time of the event.";
 elseif($_GET['error'] == "event_location") 
 $msg = "Please provide the location of the event.";
 elseif($_GET['error'] == "event_phone_number") 
 $msg = "Please provide a phone number for the venue.";
 elseif($_GET['error'] == "event_price") 
 $msg = "Please provide a price to attend the event.";
 elseif($_GET['error'] == "event_description") 
 $msg = "Please provide a description for the event.";
 elseif($_GET['error'] == "spam") 
 $msg = "Please enter correct turing code.";
 elseif($_GET['msg'] == "success") { 
 $msg = "We have received your feedback and will get back with you as soon as possible, if required.";
 else {

<!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">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Clubs Submission Form</title>

<p style="color:#ff0000;font-weight:600;"><?=$msg?></p>
<form name="Contact" action="SendMail.php" method=post>
<!--<input name="recipient" type="hidden" value="clubsub@metronews.com">-->
<!--<input name="subject" type="hidden" value="Clubs submission from Metroactive.com">-->
<!--<input name="redirect" type="hidden" value="http://www.metroactive.com/contact/thanks.html">-->

<p>Your Name:</p>
<input type=text name="name" class="class_form_field">

<p>Your E-mail Address:</p>
<input type=text name="uemail" class="class_form_field">

<p>Your Daytime Phone:</p>
<input type=text name="phone" class="class_form_field">

<p>Club Event Title:</p>

<input type=text name="event_title" class="class_form_field">

<p>Date and Time:</p>
<input type=text name="event_date_and_time" class="class_form_field">

<input type=text name="event_location" class="class_form_field">

<p>Phone Number:</p>
<input type=text name="event_phone_number" class="class_form_field">


<input type=text name="event_price" class="class_form_field">

<p>Event Description:</p>
<textarea class="class_form_field" rows=20 wrap=virtual name="event_description"></textarea>

<p>Enter Security Code:</p>

<p style="background-image:url(images/anit-spam.gif);width:150px;height:50px;"><b><?=$Str?></b> &nbsp;</p><input type="text" size="8" name="code"> 
<input type="hidden" size="8" name="hcode" value="<?=$Str?>"> 
<br /><br />
<input type=submit value="Submit Contact Form"> 
          <input type=reset value="Clear Form" size="20">

You really don't need to save anything in a cookie for this.

You can just pass on what the use has put in the form in the HTTP POST or GET and populate the form with this for the user.

It easiest if you use the same PHP script both for generating the form and processing the form. That way you don't have to pass your form fields via HTTP twice.

Simple example: (pseudo code)

$is_generate = false;

if (/* some validation failed */) {

$is_generate = true;

if ($is_generate) { // generate form

echo '<form ... >';
echo '<input type="text" name="foo" value="'.clean($_POST['foo']).'" />';

echo '... etc. etc..';
echo '</form>';

} else { // process form


// your variable cleansing funtion
function clean($var) {
// do some sanitizing
return $clean_var;

NOtice the line:

echo '<input type="text" name="foo" value="'.clean($_POST['foo']).'" />';

Ive set the value="" to the HTTP POST param "foo" since that is what was passed last time the form was filled...

The reason I have clean() function there is just to note that if you are going to print anything sent by the user, back to the browser, you will have to clean your variables with your own custom function.

usually to prevent cross-site scripting.. XSS.

Withought that lil bit it would look something like:

echo '<input type="text" name="foo" value="'.$_POST['foo'].'" />';
commented: Very well thought-out explanation +1

Hi there Dig-Eth,

Thanks sooo much for taking the time to write all that out for me.
I am pretty new at PHP, so I will take what you have given me and try to piece it together.

I sincerely appreciate your help.

Have a GREAT Thanksgiving! :)