Jump to content

Retaining information in php contact form


slaterino

Recommended Posts

Hi,

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:

 

<?php
   // start PHP session
   session_start();

    if(isset($_POST['docontact']))
    {

        $to = "[email protected]";

        $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");
        }
    }
?>

 

And these are the headers that appear if the criteria are not met:

 

        <?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>";
            }
        ?>

 

Does anyone have any suggestions how I can go about resolving this?

 

Many thanks!

Russ

Sweet. Jobs a good one. Think I've got fairly secure too!

 

I have one more query regarding this though which I was wondering if you had an answer to. The way my current form works is if someone types in an incorrect e-mail they get an error message above the form and the url is something like: "form.php&fillall". If they then got the code wrong they would then have 2 error messages displayed and url would be: "form.php&fillall&wrongcode". This goes on ad infinitum. Is there any way to delete the last addition to the header so that the user does not end up with a mass of messages above the form and a huge url?

 

Thanks

Russ

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.