Articles HTML Tutorial CSS Tutorial JavaScript Tutorial PHP Tutorial MYSQL Tutorial Contact Us
PHP Form Processor

A form processor is a php script which will receive all data submitted to it from HTML Forms and process it. By 'process' we mean... do whatever you've told it to do... add the data to a databse, or email it somewhere.

Basic Form Processor, saved as formprocessor.php, that takes data submitted by a form, and emails it to you: <?PHP
if (isset($_POST)) {

$message = "The following form was submitted from your website\n\n";
$sendto = "me@myemailaddress.com";

foreach ($_POST AS $key => $value) {
else if($key == "subject") { $subject = $value; }
else if($key == "thankyou") { $thankyou = $value; }
else if($key == "error") { $error = $value; }
else { $message .= $key.": ".$value."\n";}
}

if($subject == "") { $subject = "Website Form"; }

$headers="From: My Website<".$sendto.">\r\nReply-To: My Website<".$sendto.">";

if(mail($sendto, $subject, $message, $headers, "-f$sendto")){

if($thankyou != ""){
header("location:".$thankyou);
}

}
else{

if($error != ""){
header("location:".$error);
}

}
?>
You will have to change the first $message value to whatever you want, and change the $sendto value to whatever email address you want the form data sent to. You might also wish to change the default $subject to anything you like, if the form doesn't have a hidden subject field.
If you wish to provide a Thankyou page for your visitors, to say "Thank you for your comment" after the form is submitted, you will need to provide a full URL to your thankyou page inside a hidden form field on your form called thankyou.
If for whatever reason your email does not get sent, and you would like to inform your visitors of this, you will need to provide a full URL to your error page inside a hidden form field on your form called error.
If you have more than one form on your website, and you want to know which form was submitted, you can include a hidden form field called subject. See the form example below for how to include these hidden form fields!

Here is a sample form that you might have on your webpage: <form name="myform" method="POST" action="http://www.mywebsite.com/formprocessor.php">
<input type="hidden" name="thankyou" value="http://www.mywebsite.com/thankyou.html">
<input type="hidden" name="error" value="http://www.mywebsite.com/error.html">
<input type="hidden" name="subject" value="Order Form">
Name:<input type="text" name="Name" size="10">
Email:<input type="text" name="Email" size="20">
Your Message:<textarea name="Message" rows="10" cols="30">Enter your message here</textarea>
<input type="submit" value="Send Form"> &nbsp; <input type="reset" value="Reset">
</form>

And here is what the first Form Processor would email you, based on the form above:

From: My Website <me@mywebsite.com>
Subject: Order Form

The following form was submitted from your website

Name: The Name submitted
Email: The Email Address submitted
Message: The Message submitted