An easy and safe way for clients and visitors to contact you on your site is a contact form. Instead of giving out your email on your site and risking spam bots picking it up and using it, you can place a contact form on your site that will safely and quickly provide a form of communication between you and your site visitors. This is a tutorial on how to build a simple PHP contact form.
How to build a simple PHP Contact form
For a contact form, there are essentially two basic files you need. An HTML form, and a PHP form handler. Below are examples of two and explanations of what they do.
The HTML file
This is a basic layout. You must style it using CSS.
<form method="post" action="mail.php">
<input type="text" name="name" value="" title="" />
<input type="email" name="email" value="" title="" />
<input name="phone" value="" title="" type="text">
<select name="subject" class="select" title="">
<option value="Contact Form">Please Select</option>
<input type="submit" value="Submit" name="submit" class="submit"/>
This is a basic form layout in HTML.Your contact form may have different form fields (eg. First Name, Last Name, Where did you hear from us, etc).Your contact form may also be styled differently in regards to the buttons, input fields, font size, etc.
The essential part here is the action=”mail.php” and name=”” tags.
The file name following the action tag is the name of your PHP form handler and its location in your web site’s directory. In this example the form handler is called “mail.php” and it is located in the root directory of the website.
The name=”” tag that accompanies each form input is needed for the PHP file to know what field you are referring to. It is wise to name them something user-friendly so you have an easier time calling out the variables in the PHP file.
The PHP File
Must be named mail.php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name n Email: $email n Phone: $phone n
$recipient = 'email@example.com'; //Your e-mail here.
$subject = $_POST['subject'];
$mailheader = "From: $email rn";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo 'Thank You!';
The PHP file must be named and placed where you originally stated in the HTML page of your contact form.
This file essentially “posts” the variables that the user typed to the e-mail. It is imperative that the name inside the POST function matches up with the name you gave the variable in the HTML contact form.
The variable $formcontent then styles the email and states what forms will be displayed, making a new line (“n”) after each.
The $recipient variable is who will be receiving the email.
Next, The subject is posted, the mail header is made which is what you see as “from” when you receive an email, or the user’s email you will reply to.
Finallly, the PHP mail() function is carried out, sending all of the forms contents successfully or ending an error due to the user forgetting to fill out an input field.
Important note: This form uses no validation/code sanitation to protect from spam bots and is more about learning how the PHP variables work and what it means. If you’d like to add Captcha to your form, check out the official site: http://www.google.com/recaptcha/whyrecaptcha