Simple Website Enquiry Form

NB: Although this tutorial is still valid, I would now suggest following my tutorial for a PHP enquiry for using ReCAPTCHA. Click Here to view.

This tutorial will cover how to create a simple PHP website enquiry form. You know the ones, you fill in your details and press 'submit' and the e-mail automatically sends your customer's enquiry to you. This function will also introduce a basic 'captcha' where the user will have to answer a maths question for the e-mail to be sent. This is designed to stop 'spam' e-mails...I must admit that the 'captcha' is only 85% effective so you may want to build in something a little more sophisticated at some stage.

The first thing you will need to do is build your form. This is fairly easy to do once you have decided what information about the customer you wish to collect. This might be name, phone number, e-mail address etc. The form we will use for this tutorial captures name, e-mail and the customer's enquiry. The HTML is below;

<form action="" id="contact" method="post">
<label>Name*</label>
<input name="name" type="text" size="30" id="name">
<label>E-mail*</label>
<input name="email" type="text" size="29" id="email">
<textarea name="enquiry" cols="50" rows="5" id="enquiry">Please type your enquiry here.</textarea>
Please answer the following question in numbers: What is <?php echo $digit1;?> + <?php echo $digit2;?>?
<input name="captcha" type="text" size="3" id="captcha">
<input type="submit" name="Submit" value="Submit" id="Submit">
</form>

You will notice that there is already some php in the form itself (shown in red text) and this is to display the 'captcha' question created later in the tutorial. I have also told the form action that it will be pointing to the page itself;

<form action=""

So you will build your php before the head Tags of your HTML page.

Next we need a message to display if the 'captcha' is filled in incorrectly or a mandatory field is not completed. The HTML and php sit in a div with the class 'message';

<div class="message"><?php echo $message; ?></div>

You will of course want to style your HTML from and the 'message' class using CSS but I will leave that up to you.

Next comes the php magic. First off you will create the warning messages. I usually create the warning messages in a separate file as I have a whole raft of warnings which I have used for various applications and it is far easier to have a catalogue of these for use again and again. You will need to create a file called 'warnings.php' and then save it. The page should have this php 'array' in it;

<?php
$warnings = array (
'mandatory' => "You have not completed a mandatory field. Please go back and complete all fields with * indicated.",
'captcha' => "You have not answered the question correctly. Please try again.",
'thanks' => "Thank You, I will be in touch soon."
);
?>

Now return to your original page with the form in it. Above the head Tags you will need to include the 'warnings.php' file you have just created;

<?php
//this code includes error or warning messages
include("includes/warnings.php");

Please note that I have 'commented' the code to make it easier to understand should you need to come back to it. Comment lines start with // so the server know that it is not part of the code to be executed.

First up we must create a function used to add two numbers together for the 'Captcha' result. The code defines the 'add' function as adding 'x' and 'y' together and then returning a variable called 'total';

//this defines the function used to add the two random numbers
function add($x,$y)
{
$total=$x+$y;
return $total;
}

Next you will need to randomly generate two numbers between 0 and 9. This is done using a built in php function called 'rand'. The code is below;

//this creates the two random numbers for the captcha
$digit1 = rand(0, 9);
$digit2 = rand(0, 9);

'digit1' and 'digit2' will also appear in the form when the user opens the page and they are referenced in the form HTML created earlier.

Next we will need to add these two numbers up and feed them into a 'variable' called 'result'. The code uses the built in php function called 'add';

//this calculates what the captcha sum answer should be
$result = add($digit1,$digit2);

Now we need to reference the names in the form and then feed these into 'variables' to use in the e-mail message;

//these are the form results
$name = $_POST['name'];
$email = $_POST['email'];
$enquiry = $_POST['enquiry'];
$formsub = $_POST['Submit'];
$captcha = $_POST["captcha"];
//this should convert answer in the captcha into a number rather than text
$test = is_numeric($captcha);

You may notice that the 'captcha' result is fed through a php function called 'is_numeric'. The reason for this is simple. The form would work normally if the maximum answer to the 'captcha' question is 9. However, as the maximum answer is 18 we have had to state that the figure returned in the 'captcha' is numerical otherwise it will take the any answer with two digits as being a string. For example 1,2 is not the same as 12 (twelve).

Now comes the final part. The next piece of code will check that the mandatory fields on the form have been filled in, check the 'captcha' has been answered correctly and then create a mail message to be sent to your inbox.

//this fires if everything is completed correctly
if($test == $result) {
if($name && $email){
//this creates the body of the mail message
$mailMessage = "The message is: You have a message from the website\n\n";
$mailMessage .= "Name:" .$name."\n\n";
$mailMessage .= "E-Mail:" .$email."\n\n";
$mailMessage .= "Enquiry:" .$enquiry."\n\n";
$header = "From: 'yourname@yourwebsite.com'\r\n";
$header .= "Reply-To: 'yourname@yourwebsite.com'\r\n";
$header .= "Return-Path: 'yourname@yourwebsite.com'\r\n";
mail('yourname@yourwebsite.com', 'Website Enquiry', $mailMessage,$header);

$message = $warnings['thanks'];
}

elseif($formsub) {
$message = $warnings['mandatory'];


}
}
elseif($formsub) {
$message = $warnings['captcha'];
}
?>

the only parts you will really need to change are the e-mail addresses in red. Change them to your e-mail address! Hopefully you can see the logic behind how the form and php work together. First it checks that the 'captcha' answer is correct, then it makes sure that the customer has filled in their name and e-mail address so that you can contact them back. Then the mail message is created and mailed to your chosen address. The three warnings which we created earlier are set to 'fire' if any of the conditions we have aren't met.

I hope that you will find this pretty simple to create and use...even if you don't fully understand why it is doing what it does! I would thoroughly recommend a good course of php lessons to anyone who is serious about web development. Php lessons are available from my good friend (and teacher) David Jackson at codeZenith php lessons.

Advertisements If you would like to advertise on Corrosive Online then get in touch through my Contact Page for details.
Join The Mailing List
    If you have found anything on this site useful or entertaining or you have enjoyed one of my Online Web Design Lessons then I would love you to make a donation. Use the Paypal button below to make a donation.