Jump to content

Form with CAPTCHA


bravo14
 Share

Recommended Posts

Hi guys

 

I found a tutorial on webcheatsheet.com, to create a CAPTCHA, and have got the following code for the form

 

form id="frmCaptcha" name="frmCaptcha">
<table> 
  <tr>
  <td><label for="name">Name</label></td>
  <td><input name="name" type="text" maxlength="20" size="32"/></td></tr>
<tr>
  <td><label for="email">Email Address</label></td>
  <td><input name="email" type="text" maxlength="60" size="32"/></td></tr>
  <tr>
<tr>
  <td valign="top"><label for="question">Question</label></td>
  <td class="textarea"><textarea name="question" cols="25" rows="10"></textarea></td></tr>
  <tr>
  <td></td>
    <tr>
    <td align="left">
      <label for="captcha">Security</label>
    </td>
    <td>
      <input id="txtCaptcha" type="text" name="txtCaptcha" value="" maxlength="10" size="32" />
    </td></tr>
    <tr>
    <td>
    </td>
    <td> 
      <img id="imgCaptcha" src="create_image.php" />
    </td>
  </tr>
  <tr>
    <td> </td>
    <td>
      <input id="btnCaptcha" type="button" value="Captcha Test" name="btnCaptcha" 
          onclick="getParam(document.frmCaptcha)" />
    </td>
  </tr>
</table> 

<div id="result"> </div>
</form>

 

 

The following is the javascript for when the form is submitted

 

/Called every time when form is perfomed
function getParam(theForm) {
//Set the URL
var url = 'captcha.php';
//Set 
up the parameters of our AJAX call
var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value )+
"&" + theForm.name.name + "=" + encodeURIComponent( theForm.name.value ) +
"&" + theForm.email.name + "=" + encodeURIComponent( theForm.email.value ) +
"&" + theForm.question.name + "=" + encodeURIComponent(theForm.question.value);

 

and the process captcha.php referenced in the javascript

 

<?
/*
This is the back-end PHP file for the How to Create CAPTCHA Protection using PHP and AJAX Tutorial

You may use this code in your own projects as long as this 
copyright is left in place.  All code is provided AS-IS.
This code is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

For the rest of the code visit http://www.WebCheatSheet.com

Copyright 2006 WebCheatSheet.com	
*/

//Continue the session
session_start();

//Make sure that the input come from a posted form. Otherwise quit immediately
if ($_SERVER["REQUEST_METHOD"] <> "POST") 
die("You can only reach this page by posting from the html form");

//Check if the securidy code and the session value are not blank 
//and if the input text matches the stored text
if ( ($_REQUEST["txtCaptcha"] == $_SESSION["security_code"]) && 
    (!empty($_REQUEST["txtCaptcha"]) && !empty($_SESSION["security_code"])) ) {
  $name = $_REQUEST['name'];
  $email = $_REQUEST['email'];
  $question = $_REQUEST['question'];


// Validation
if(2 < strlen($name) && 2 < strlen($email) && 2 < strlen($question))
{

$email_message = <<< EMAIL
A message was received from $name.

Name: $name
Email:$email

Question:
$question

EMAIL;

$headers = "from:$email\r\n"; //Set from address


if(mail('mark@bravo14.co.uk','Contact from projectrenovations.co.uk', $email_message, $headers)) //Change email address accordingly
{
	echo "Thank you $name, your email has been delivered, we will contact you shortly.";	
}
else 
{
	echo "Sorry $name, we had a problem sending the email.";
}


}
else
{
  echo "You did not fill in the form properly. Please use the browser's 'back' button and update the form.";	
}
;
} else {
  echo "You have entered the wrong security code, try again!";
}
?>

 

however when I submit the form, it doesn't do anything, any ideas?

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.