Remembering values for a contact form error page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Remembering values for a contact form error page

Post by tomsace »

Hey,
I have created a contact form page on my website. It all works correctly but the thing I want to do is when there is an error in my form such as incorrect format for email address or missing field I want the error page to say 'Error. Please fill in all fields correctly and try again:' and below have the same form. So far I have done this, but the problem is the fields are all blank and the user would have to start again.

How can I remember the values from the previous page and have them pre-filled in on the error page?

This is the code for the contact form. (contact.php)

Code: Select all

<center><table><tr><td>
 
Please fill out all fields.<br><br>
 
 
<font size="2">
<form method="post" action="sendeail.php">
 
<!-- DO NOT change ANY of the php sections -->
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>
 
<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
 
 
Your Name: <br />
<input class="input" type="text" name="visitor" size="35" />
<br /><br>
Your Email:<br />
<input class="input" type="text" name="visitormail" size="35" />
<br /><br>
Reason for Contacting:<br />
<select class="input" name="attn" size="1" style="width: 200px;">
<option value=" General Question ">General Question</option>
<option value=" Re: Free Quote ">Re: Free Quote</option>
<option value=" Technical Support ">Technical Support</option>
<option value=" Other ">Other</option>
</select>
<br /><br />
Message:
<br />
<textarea class="input" name="notes" rows="4" cols="40"></textarea>
<br /><br>
<input class="input" type="image" src="/images/btn_send.jpg" value="Send" />
</form>
 
</td></tr></table>
This is the code for the php send page. (sendeail.php)

Code: Select all

<?php
 
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];
 
 
if (eregi('http:', $notes)) {
die ("Do NOT try that! ! ");
}
if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
header( 'Location: http://www.hiqmods.com/index.php?id=error' );
$badinput = header( 'Location: http://www.hiqmods.com/index.php?id=error' );
echo $badinput;
die (" ");
}
 
if(empty($visitor) || empty($visitormail) || empty($notes )) {
header( 'Location: http://www.hiqmods.com/index.php?id=error' );
die (" ");
}
 
 
$attn = $attn ;
$subject = $attn;
 
$notes = stripcslashes($notes);
$message = "$todayis From: $visitor\n
Email: $visitormail \n
Subject: $attn \n
Message: $notes \n
IP: $ip \n
Browser: $httpagent \n
";
 
$from = "From: contact@hiqmods.com\r\n";
 
 
mail("tstanniland@yahoo.com", $subject, $message, $from);
 
header('Location: index.php?id=sent');
?>
And finally this is the error page. (error.php)

Code: Select all

<font class="Error">Error. Please fill in all fields correctly and try again:<br><br>
 
<center><table><tr><td>
 
Please fill out all fields.<br><br>
 
 
<font size="2">
<form method="post" action="sendeail.php">
 
<!-- DO NOT change ANY of the php sections -->
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>
 
<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
 
 
Your Name: <br />
<input class="input" type="text" name="visitor" size="35" />
<br /><br>
Your Email:<br />
<input class="input" type="text" name="visitormail" size="35" />
<br /><br>
Reason for Contacting:<br />
<select class="input" name="attn" size="1" style="width: 200px;">
<option value=" General Question ">General Question</option>
<option value=" Re: Free Quote ">Re: Free Quote</option>
<option value=" Technical Support ">Technical Support</option>
<option value=" Other ">Other</option>
</select>
<br /><br />
Message:
<br />
<textarea class="input" name="notes" rows="4" cols="40"></textarea>
<br /><br>
<input class="input" type="image" src="/images/btn_send.jpg" value="Send" />
</form>
 
</td></tr></table>
Thanks alot.
Tom.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Remembering values for a contact form error page

Post by Bill H »

I put both the php action and the form in the same script

Code: Select all

 
if (isset($_POST['email']))
{
  // all of the stuff that sends the mail
}
else
{
    // html form here with 
   echo "<input  type=text name='whatever' value='",$_POST['Name'],"'>";
}
 
 
 
If the post was not mande the $_POST is empty and the field is blank. If returning from an error allof the previous entries are shown, as they are the $_POST vars.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Remembering values for a contact form error page

Post by tomsace »

Hey,

Thanks for the reply, I have tried what you said but couldnt get it working... Unless I was doin it wrong??

Could you please explain in a bit more detail please Im quite new to php still!!

Thanks,
Tom.
~BlitZ
Forum Newbie
Posts: 22
Joined: Mon Feb 23, 2009 5:36 pm
Location: United Kingdom

Re: Remembering values for a contact form error page

Post by ~BlitZ »

Could you maybe try saving the values in a session?
Try putting the form information inside a session then returning it to the browser on the next page if it's wrong?
Just a thought.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Remembering values for a contact form error page

Post by Bill H »

$_SESSION is another useful way to do it, and a little more durable, but may be more than is required based on the question in the original post. Consider this, which may have a few flaws, as I cobbled it together rather quickly

Code: Select all

 
<?php
 
if (isset)$_POST['ip']))
{
     $visitor = $_POST['visitor'];
     $visitormail = $_POST['visitormail'];
     $notes = $_POST['notes'];
     $attn = $_POST['attn'];
     $error = 0; 
       
     if (eregi('http:', $notes)) $error = 1;
     
     if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) $error = 2;
       
     if(empty($visitor) || empty($visitormail) || empty($notes )) $error = 3;
      
     if (!$error)
     {
          $attn = $attn ;
          $subject = $attn;
           
          $notes = stripcslashes($notes);
          $message = "$todayis From: $visitor\n
          Email: $visitormail \n
          Subject: $attn \n
          Message: $notes \n
          IP: $ip \n
          Browser: $httpagent \n
          ";
           
          $from = "From: contact@hiqmods.com\r\n";
           
           
          mail("tstanniland@yahoo.com", $subject, $message, $from);
 
          $Go = "Location:" . $_SERVER['PHP_SELF'] . "Sent=1";
          header($Go);
     }
}
if (isset($_GET['Sent']))
{
     // html to indicate message sent
}
else
{
?>
     <center><table><tr><td>
      
     Please fill out all fields.<br><br>
     <?php if (isset($_POST['ip'])) echo "Alarm!"; ?>
      
     <font size="2">
     <form method="post" action="sendeail.php">
       
     <!-- DO NOT change ANY of the php sections -->
     <?php
     $ipi = getenv("REMOTE_ADDR");
     httprefi = getenv ("HTTP_REFERER");
     $httpagenti = getenv ("HTTP_USER_AGENT");
     ?>
 
     <input type="hidden" name="ip" value="<?php echo $ipi ?>" />
     <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
     <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
 
      
     Your Name: <br />
     <input class="input" type="text" name="visitor" size="35" value="<?php echo $_POST['visitor'] ?>"/>
     <br /><br>
     Your Email:<br />
     <input class="input" type="text" name="visitormail" size="35" <?php echo $_POST['visitormail'] ?>/>
     <br /><br>
     Reason for Contacting:<br />
     <select class="input" name="attn" size="1" style="width: 200px;">
     <option value=" General Question ">General Question</option>
     <option value=" Re: Free Quote ">Re: Free Quote</option>
     <option value=" Technical Support ">Technical Support</option>
     <option value=" Other ">Other</option>
     </select>
     <br /><br />
     Message:
     <br />
     <textarea class="input" name="notes" rows="4" cols="40"><?php rcho $_POST['notes'] ?></textarea>
     <br /><br>
     <input class="input" type="image" src="/images/btn_send.jpg" value="Send" />
     </form>
       
     </td></tr></table>
<?php
}
?>
 
The first section is what sends the email. It then calls the same script with a $_GET appended that activates the second section which contains html to advise that the email was sent (I didn't fill that in). The third section runs if neither the $_POST or $_GET is present and provides the form. It will also run if the $POST was present but $error prevented the email from being sent. The $_POST vars serve as defaults for the inputs, and if the $_POST is not present they are merely nulls.

I didn't put in the $_POST segments to set the selected item in the dropdown, but you should be able to figure what is needed there.

Hope that makes it a bit more clear
~BlitZ
Forum Newbie
Posts: 22
Joined: Mon Feb 23, 2009 5:36 pm
Location: United Kingdom

Re: Remembering values for a contact form error page

Post by ~BlitZ »

Aha, Good thinking ;), I love this community :D
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Remembering values for a contact form error page

Post by tomsace »

Hey,

Thanks alot for helping.
Just a few bugs to sort but I ironed them out...
BUT, its not working? Just refreshes the form blank again!? The error appears and everything but the form still returns blank.

Heres the code I ended with after sorting a few bugs:

Code: Select all

<?php
 
if (isset($_POST['ip']))
{
     $visitor = $_POST['visitor'];
     $visitormail = $_POST['visitormail'];
     $notes = $_POST['notes'];
     $attn = $_POST['attn'];
     $error = 0;
       
     if (eregi('http:', $notes)) $error = 1;
     
     if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) $error = 2;
       
     if(empty($visitor) || empty($visitormail) || empty($notes )) $error = 3;
     
     if (!$error)
     {
          $attn = $attn ;
          $subject = $attn;
           
          $notes = stripcslashes($notes);
          $message = "$todayis From: $visitor\n
         Email: $visitormail \n
         Subject: $attn \n
         Message: $notes \n
         IP: $ip \n
         Browser: $httpagent \n
         ";
           
          $from = "From: contact@hiqmods.com\r\n";
           
           
          mail("tstanniland@yahoo.com", $subject, $message, $from);
 
          $Go = "Location:" . $_SERVER['PHP_SELF'] . "Sent=1";
          header($Go);
     }
}
if (isset($_GET['Sent']))
{
     // html to indicate message sent
}
else
{
?>
     <center><table><tr><td>
     
     Please fill out all fields.<br><br>
     <?php if (isset($_POST['ip'])) echo "Alarm!"; ?>
     
     <font size="2">
     <form method="post" action="sendeail.php">
       
     <!-- DO NOT change ANY of the php sections -->
     <?php
     $ipi = getenv("REMOTE_ADDR");
     $httprefi = getenv ("HTTP_REFERER");
     $httpagenti = getenv ("HTTP_USER_AGENT");
     ?>
 
     <input type="hidden" name="ip" value="<?php echo $ipi ?>" />
     <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
     <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
 
     
     Your Name: <br />
     <input class="input" type="text" name="visitor" size="35" value="<?php echo $_POST['visitor'] ?>"/>
     <br /><br>
     Your Email:<br />
     <input class="input" type="text" name="visitormail" size="35" <?php echo $_POST['visitormail'] ?>/>
     <br /><br>
     Reason for Contacting:<br />
     <select class="input" name="attn" size="1" style="width: 200px;">
     <option value=" General Question ">General Question</option>
     <option value=" Re: Free Quote ">Re: Free Quote</option>
     <option value=" Technical Support ">Technical Support</option>
     <option value=" Other ">Other</option>
     </select>
     <br /><br />
     Message:
     <br />
     <textarea class="input" name="notes" rows="4" cols="40"><?php echo $_POST['notes'] ?></textarea>
     <br /><br>
     <input class="input" type="image" src="/images/btn_send.jpg" value="Send" />
     </form>
       
     </td></tr></table>
<?php
}
?>
Thanks alot,
Tom.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Remembering values for a contact form error page

Post by Bill H »

Well, I just copied the code you posted and, after correcting the "action" name on line 53, it works fine for me. The word "Alarm!" appears, and the words that I had typed into the fields are still in the fields. I'm not sure why you weren't getting a "file not found" error (as I did), since the form action had the wrong filename. So if it's going to a file that is not actually named in the action line, and sending the $_POST['ip'] but not the other $_POST vars, then something is seriously wrong with your server.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Remembering values for a contact form error page

Post by tomsace »

Hey,

I must have missed the action command. What should I type in the action command as I cant get the form to send, It replies with the word 'Alert!' when there is an error, but even when all is filled out correctly, I still recieve the error message, but the message still sends!
Also I didn't recieve file not found error because I did actually have a file names sendeail.php on my server.

Thanks.
Tom.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Remembering values for a contact form error page

Post by Bill H »

Well, what is the name of the file you are using? The name in the form action should be the same as the name of the file itself.

The script works on my server when I paste it from the code you posted and correct the filename, so you are having a problem with filenames. I have done everything but come to your house and do it on your computer for you. I was giving you a process, not a completed script for you to put on your server and use.

The alarm line should actually be if ($error > 0) echo "Alarm!"; but you should have been able to figure that out for yourself.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Remembering values for a contact form error page

Post by tomsace »

Like I said...
Im quite new to php still!!
which kinda means I don't really know what I am doing because i'm new!!!

Anyway changed the Alarm! code to if ($error > 0) echo "Alarm!" which works better but once the error code has already been shown, and the user fills out the form properly and sends again, they dont get redirected to the sent=1 page but the form is still been sent, but the user doesn't know this...
Note: If the user fills the form out correctly the first time they are sent to the sent=1 page which I have noticed in the code.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Remembering values for a contact form error page

Post by Bill H »

There's nothing wrong with being new to php, but simply getting other people to write scripts for you is not the best way to learn. You should be finding ways to know what the "location header" does and how it works, what the question mark does in the url and how it relates to the $_GET superglobal variable array, what happens to variables in a script flow, and what the elements in html tags are and what role they play.
What should I type in the action command
If you know what all of the elements of a <form> tag are and what the "action" element does, you should be able to figure that out quite easily.

I'm quite willing to help by providing process suggestions and explaining where your script has errors, offering functions that you may not have encountered (although the php manual is quite readable), and suggesting improvements. But I'm just not in the business of scripting your site for you for free.
Post Reply