PHP contact form, with gotcha!">Easy PHP contact form, with gotcha!
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.
-
{
-
$name = $_POST['name'];
-
$email = $_POST['email'];
-
$stuff = $_POST['stuff'];
-
$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.
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.
-
$to_email = "you@yourdomain.com";
-
$subject = "$name wanted to tell you something";
-
$msg = "$name wanted to let you know $stuff<br />Email them at <a href='mailto:$email'>$email</a> to answer!";
-
-
$mailheaders = "From: $email\n";
-
$mailheaders .= "Reply-To: $email\n";
-
$mailheaders .= "Return-Path: $email\n";
-
$mailheaders .= "Content-type: text/html\n\n";
-
-
echo "<script>alert('Thanks for taking the time to contact me, $name I will get back to you as soon as possible!');</script>";
-
echo '<script>location.replace("/")</script>';
-
exit;
-
} 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.
-
$form = '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
-
$form .= 'And you are?<br />
-
<input type="text" name="name" id="name" /><br />
-
I can email you where?<br />
-
<input type="text" name="email" id="email" /><br />
-
You\'d like to tell me?<br />
-
<textarea name="stuff" id="stuff"></textarea><br />
-
ABC is easy as <input type="text" name="human" id="human" size="3" /> (<em>According to the Jackson 5</em>)<br />
-
<input name="submit" type="submit" value="Tell Me Something Good!" />
-
</form>';
-
-
echo $form;
-
}
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.
Posted in PHP having 1 comment »


May 21st, 2008 at 2:46 pm
[…] 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 […]