Page 1 of 1

How to add a simple CAPTCHA to my Form2Email script

Posted: Sun Jul 24, 2011 9:44 am
by Yiaggi
Hi guys,

I have a simple HTML form which uses an even simpler PHP Form2Email script to process it.

I would like to add a CAPTCHA to the form so that I can protect against rogue emails. I have tried 4 or 5 different methods online and seem to keep getting the same result - a blank screen when submit is pressed (instead of my "Thank You") message.

I really just want to have the CAPTCHA in the form before submit or when submit is pressed a CAPTCHA appear before the form. Can any one help?

Ta in advance .... Scripts are below:

<HTML>
<form id="ContactForm2" method="post" action="FormToEmail2.php" class="form3">

Your Email<br /><label><input name="email" type="text" id="email" size="34" maxlength="80" class="input2"/><br />

</label>

Your message<label>

<textarea name="comments" class="input2" cols="26" rows="3" id="comments"></textarea>

</label>

<label>

<input type="submit" name="submit" id="submit" value="Submit" />

</label>

</form>

and now the PHP Form2Email:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<?php

function treat_submission()

{

	$email = $_POST['email'];

	$comments = $_POST['comments'];

	$my_email = "myemail@mydomain.com"; // this is my "admin" email	

	//	MESSAGE TO ADMIN:

	$headers = "To: ".$my_email. "\r\n";

	$headers .= "From: ".$email. "\r\n";

	$subject = "Request / Comment via your website";

	$msg = "User ".$email." has submitted a request / comment as follows:\r\n\r\n"; 

	$msg.= "Message - ".$comments."\r\n\r\n";

	mail($my_email, $subject, $msg, $headers);

// MESSAGE TO VISITOR:

	$headers = "To: ".$email. "\r\n"; // this is the email given by the visitor

	$headers .= "From: ".$my_email. "\r\n";

	$headers .= "MIME-Version: 1.0" . "\r\n";

    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

	$subject = "Thank you for your comment / request to Me.";

	$msg = "

<html>

<head>

<title>Thank You! </title>

</head>

<body>

<p>This is an automated response<br /><br />Thank you very much for submitting a comment! <br /><br />If your message requires a response then will will do so as soon as possible.<br><br/>



Kind regards<br><br>

Me

</p>

</body>

</html>

";

mail($email,$subject,$msg,$headers);

	}

?>

</head>

<body>

<?php

	if (isset($_POST['submit']))

		echo (treat_submission($_POST));

		

	else

		echo ("<p align=\"center\">The form has not been received.</p>");

?>

<div>

<center>

<p><b>Thank you <?php print stripslashes($_REQUEST['name']); ?></b>

    <br>

  </p>

<p>Your message has been sent to me</p>

<p>We will respond as soon as possible</p>

<p><a href="http://www.mywebsite.com">Click here to return to our home page</a></p>

</center>

</div>

</body>

</html>

I keep coming across scripts requiring me to add code to both parts of the form. Whenever I do this my form stops processing! Please help Guru's!!

Re: How to add a simple CAPTCHA to my Form2Email script

Posted: Tue Jul 26, 2011 4:06 am
by Benjamin
ReCaptcha is very easy to implement. I believe it requires the GD library. Most hosting providers offer that now though. If you run into issues with things breaking you should be checking the error log to determine the cause.