Easy PHP contact form, with gotcha!

May 19th, 2008 by Jack Keller

Yes that’s right, I called it Gotcha. So why Gotcha? Simple really, it’s not as involved as a Captcha (also not as secure but will fend off most spambots). Below I have written a very simple Contact form in PHP, I will explain the goods right after the form.

Okay, let’s take a step through this method. First off is the if(isset($_POST[‘submit’])), all that really does it tell the page to run the mail and spam checking functions if the user submitted the form. Then it gets the POST methods from the submission and we set the variables.

PHP:
  1. if(isset($_POST[‘submit’]))
  2. {
  3. $name = $_POST[‘name’];
  4. $email = $_POST[‘email’];
  5. $stuff = $_POST[‘stuff’];
  6. $human = $_POST[‘human’];

The if ($human != “123”) { business checks that the user is not a spambot.

In this case ABC is easy as 123, so if the user doesn’t type in 123 it will alert them and send them back to the previous page (I hate to click a back button, so I took that step out). If they do answer 123 the script will hit the next block, which is a basic PHP mail function. If they did not it alerts them and sends a history.back(1) script so they can take another whack at it.

PHP:
  1. if ($human != “123”) {
  2.  
  3.   echo “<script>alert(‘Whoa $name you either answered the last question wrong, or forgot to answer at all!’);”;
  4.   echo ‘</script><script>history.back(1)</script>’;
  5.   exit;
  6. }

If the user did answer the secret question correctly, the script can move onto the next step. In this step the users comments will be sent to you. So I set a variable for your email (in this example you@yourdomain.com, pretty basic I know). Then the subject and message body. Tip, you can inject the variables from the first step into your subject and message, in fact you need to somewhere or you won’t get any info. You do this by using the $name, $email, etc.

PHP:
  1. $to_email = “you@yourdomain.com”;
  2. $subject = “$name wanted to tell you something”;
  3. $msg = “$name wanted to let you know $stuff<br />Email them at <a href=’mailto:$email’>$email</a> to answer!”;
  4.  
  5. $mailheaders = “From:  $email\n;
  6. $mailheaders .= “Reply-To:  $email\n;
  7. $mailheaders .= “Return-Path: $email\n;
  8. $mailheaders .= “Content-type: text/html\n\n;
  9. mail($to_email, $subject, $msg, $mailheaders);
  10.  
  11.   echo “<script>alert(‘Thanks for taking the time to contact me, $name I will get back to you as soon as possible!’);</script>”;
  12.   echo ‘<script>location.replace("/")</script>’;
  13.   exit;
  14. } else {

Cool, the form was sent and the user was informed of their success, and then sent to the site root. The } else { is fairly important, without this nothing will show up for the user to fill out.

PHP:
  1. $form = ‘<form action="’.$_SERVER[‘PHP_SELF’].‘" method="post">’;
  2. $form .= ‘And you are?<br />
  3.     <input type="text" name="name" id="name" /><br />
  4.     I can email you where?<br />
  5.     <input type="text" name="email" id="email" /><br />
  6.     You'd like to tell me?<br />
  7.     <textarea name="stuff" id="stuff"></textarea><br />
  8.     ABC is easy as <input type="text" name="human" id="human" size="3" /> (<em>According to the Jackson 5</em>)<br />
  9.     <input name="submit" type="submit" value="Tell Me Something Good!" />
  10. </form>’;
  11.  
  12. echo $form;
  13. }

Lastly, do not forget to put your } before the end of the script, I say that because I sometimes forget that.

So that’s it, if you want to download this script for reference click here If you just copy and paste the highlighted code you’ll have to replace all ’ with the proper single ticks.

Share? These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • StumbleUpon
  • del.icio.us
  • NewsVine
  • Technorati
  • TwitThis
  • Google
  • Facebook
  • description

Posted in PHP

One Response

  1. php html br

    […] Below I have written a very simple Contact form in PHP, I will explain the goods right after thttp://www.mediaextra.net/2008/scripting/php/easy-php-contact-form-gotcha/Creazione siti internet e fotografo a Mestre e VeneziaRealizzazione siti internet e fotografo a […]

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

About media|extranet

We're a group of industry professionals who would like to give a little bit back to the community. Although we've just begun, we plan to do some write-ups on Advertising, Marketing, Development (.NET, PHP, Flash, Flex, AIR, JavaScript Frameworks, etc.) and Copywriting. Visit our contact section if you would like to see us cover something that we're not. The links below are for our writers, visit them directly if you have a project in mind that could utilize their talents.

Visit Snackbox Visit ☆dup