Every website needs a way of contacting the people behind it and putting an email on a page is not such a good idea; because bots can easily pick up this email address and send spam to it. This is where a contact form comes in very useful because people can send you messages but do not get your email address.
For a contact form the first thing that we need to do is to create a form using HTML so that they can input their information and message that they are going to send.
<form action="send_message.php" method="POST"> Name: <input type="text" name="name"> <br /> Email: <input type="text" name="email"> <br /> Message: <textarea name="message"></textarea> <br /> <input type="submit" name="send_message" value="Send!"> </form>
When the press send it will then go to a page called send_message.php. This page will grab the information that they input and send it to an email address using the mail() function in PHP.
<?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $to = 'your_address@your_email.com'; $subject = "New message from $name"; mail($to, $subject, $message, "From: $email"); echo "Message sent"; ?>
We need to check that the sender of the message is human so we will add a simple math question to the bottom of our form.
<?php $num_one = rand() % 10; $num_two = rand() & 10; $final_num = $num_one + $num_two; $_SESSION['answer'] = $final_num; echo $num_one . ' + ' . $num_two . ' = '; ?> <input type="text" name="answer" /> <br />
Don't forget to add
<?php session_start(); ?>
To the top of the page or we will get errors.
Now we check to see if they have entered information into every field and that the answer to the math question is correct, if it is the message will be sent otherwise an error will be returned.
All the code for the form is:
<?php session_start(); ?> <form action="send_message.php" method="POST"> Name: <input type="text" name="name"> <br /> Email: <input type="text" name="email"> <br /> Message: <textarea name="message"></textarea> <br /> <?php $num_one = rand() % 10; $num_two = rand() & 10; $final_num = $num_one + $num_two; $_SESSION['answer'] = $final_num; echo $num_one . ' + ' . $num_two . ' = '; ?> <input type="text" name="answer" /> <br /> <input type="submit" name="send_message" value="Send!"> </form>
All the code for error checking and sending the message is:
<?php session_start(); $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $user_answer = $_POST['answer']; $real_answer = $_SESSION['answer']; $to = 'your_address@your_email.com'; $subject = "New message from $name"; if(empty($name) OR empty($email) OR empty($message)) { echo "Fill in all fields."; } elseif($user_answer != $real_answer) { echo "Math question was incorrect, please try again"; } else { mail($to, $subject, $message, "From: $email"); echo "Message sent"; } ?>
You can download the source code for this tutorial here.
Please leave your comments and suggestions below for this tutorial.
Related posts:
- Adding security to CodeIgniter forms with a custom library class The class that we are going to create within CI...
- Build a Twitter-like site with CodeIgniter and jQuery Part 2 Don't forget to check out part 1 if you have...
- Uploading files to Rackspace Cloud (Mosso) using PHP API Rackspace Cloud is a great VPS service, but when I...
- Send form data to PHP without the whole page reloading using jQuery This is my screencast for the Nettuts/Screenr screencast competition. I...
Related posts brought to you by Yet Another Related Posts Plugin.

Very nice!
The download files have more files than this tut. I hope the next one will explain about these files in details.
Thanks.
Nice tutorial for beginners
, maybe add a brief explenation on how to filter the inputed data? (some security).
if you are interested you can submit your tutorials at http://cmstutorials.org, it's brand new and could use interesting tutorials like yours
nice post i really appreciate it