PHP contact form, with gotcha!">Easy PHP contact form, with gotcha!

May 19th, 2008 by Jack Keller

Yes that's right, I called it Gotcha. So why Gotcha? Sim­ple really, it's not as involved as a Captcha (also not as secure but will fend off most spam­bots). Below I have writ­ten a very sim­ple Con­tact 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 check­ing func­tions if the user sub­mit­ted the form. Then it gets the POST meth­ods from the sub­mis­sion and we set the variables.

PHP:
  1. if(isset($_POST['sub­mit']))
  2. {
  3. $name = $_POST['name'];
  4. $email = $_POST['email'];
  5. $stuff = $_POST['stuff'];
  6. $human = $_POST['human'];

The if ($human != "123") { busi­ness 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 pre­vi­ous page (I hate to click a back but­ton, so I took that step out). If they do answer 123 the script will hit the next block, which is a basic PHP mail func­tion. 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 ques­tion wrong, or for­got to answer at all!');";
  4.   echo '</script><script>history.back(1)</script>';
  5.   exit;
  6. }

If the user did answer the secret ques­tion cor­rectly, the script can move onto the next step. In this step the users com­ments will be sent to you. So I set a vari­able for your email (in this exam­ple you@yourdomain.com, pretty basic I know). Then the sub­ject and mes­sage body. Tip, you can inject the vari­ables from the first step into your sub­ject and mes­sage, in fact you need to some­where or you won't get any info. You do this by using the $name, $email, etc.

PHP:
  1. $to_email = "you@yourdomain.com";
  2. $sub­ject = "$name wanted to tell you some­thing";
  3. $msg = "$name wanted to let you know $stuff<br />Email them at <a href='mailto:$email'>$email</a> to answer!";
  4.  
  5. $mail­head­ers = "From:  $email\n";
  6. $mail­head­ers .= "Reply-To:  $email\n";
  7. $mail­head­ers .= "Return-Path: $email\n";
  8. $mail­head­ers .= "Content-type: text/html\n\n";
  9. mail($to_email, $sub­ject, $msg, $mail­head­ers);
  10.  
  11.   echo "<script>alert('Thanks for tak­ing the time to con­tact 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 suc­cess, and then sent to the site root. The } else { is fairly impor­tant, with­out this noth­ing 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 Jack­son 5</em>)<br />
  9.     <input name="submit" type="submit" value="Tell Me Some­thing Good!" />
  10. </form>';
  11.  
  12. echo $form;
  13. }

Lastly, do not for­get to put your } before the end of the script, I say that because I some­times for­get that.

So that's it, if you want to down­load this script for ref­er­ence click here If you just copy and paste the high­lighted code you'll have to replace all ' with the proper sin­gle ticks.

Share?
  • Facebook
  • Design Float
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Digg

Posted in PHP having 1 comment »

One Response

  1. php html br

    […] Below I have writ­ten a very sim­ple Con­tact form in PHP, I will explain the goods right after thttp://www.mediaextra.net/2008/scripting/php/easy-php-contact-form-gotcha/Creazione siti inter­net e fotografo a Mestre e VeneziaRe­al­iz­zazione siti inter­net e fotografo a […]

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