Saturday, December 31, 2011

Simple PHP Contact Form

Today I added a simple contact form to our website. I used PHP and thought I would share how easy it was to do.


Requirements:
  • Ability to edit html files for you site
  • PHP running on your server
  • Text Editor
Below is the HTML form, this can be directly pasted into your site and edited as needed:

Name:
Email :
Phone Number:
Phone Number 2 :
Budget:
Details:

Sorry this is a little jumbled in my code box here, it may be easier to paste it into an editor and review it there for readability.

Note: <table> tags are frowned upon in favor of <div>'s these days, or in this case, <fieldset> and <label>. Also, shame on me for using inline styles right? (using style=" styles " inside the HTML tags). I used these mainly so that the code can be copy pasted into an existing site without meddling with the css of the site. It is more plugin-able than if using CSS and fieldsets and labels.

Take special note of the action="/email-sent.php" inside the <form> element. This is the file that will "catch" our form when it is submitted to the server and will mail the contact information to you. Also note the names and types of fields included in the form. These will be referenced in the email-sent.php file.

Create a new file and name it "email-sent.php", or whatever you want the action="" file to be called.

$subject = "Web Services Inquiry"; 
$message = $_POST['name'] . "\n\n" . $_POST['phone1'] . "\n\n" . $_POST['budget'] . "\n\n" . $_POST['details'];
$mail_from=$_POST['customer_mail']; 
$header="from: " . $_POST['name'] . "<" . $mail_from . ">";
$to ='YOUREMAIL@domain.com';
if ($_POST['phone2']){
   $send_contact = FALSE;
}
else {
   $send_contact=mail($to,$subject,$message,$header);
}

// Display a mesage to notify visitors
if($send_contact){
echo "Thank you for contacting us, we have recived your information.
";
}
else {
echo "ERROR";
}

Plop this onto your server and edit the $to email to be your own email that you want contact information sent to. If you want to edit the form fields in the html, make sure that you also edit the php so that they correlate with each other, every form field used should have a corresponding variable in the php and be included in some way in the $message.

The dots ( . ) separating the variables in my $message field are PHP's way of concatenating strings. The "\n\n" strings add two newlines between variables so that the email is somewhat formatted.

It will appear like this:
Name
(two lines)
Phone Number
(two lines)
Budget
(two lines)
Details

NOTE: HONEYPOT
Bots WILL crawl over your site and put spam information into the form and send it to you. Left unchecked these bots will fill your email inbox with lovely junk so let me explain the honeypot. The honeypot bot defense lies in my phone2 field, called the honeypot field. In my separate css file I hide the phone2 field using the following code:

tr#fpn {
   display: none;
}

The phone2 field will not be visible to a user when they visit the page and they will not enter information into it. However, when a bot crawls the page they will see the field in the code and will fill it out when they fill out the form. On line 6 of the PHP code I check to see if there is a value entered in phone2. If there is a value, no email is sent and "ERROR" is displayed on the page. Bots find the honeypot, users do not.

In the end when the form is filled out correctly and sent it generates an email message like the following. Hopefully the visitors that send you contact information are more helpful than the Snap Dazzler dude that sent this one.

1 comment:

  1. Wonderful post.Thanks for sharing this information to my vision which was very informative.
    Php Developers Chennai | Php Developers India


    ReplyDelete