Trying to add captcha to current ajax form

Hi I am trying to get captcha to work with my current form

my form page

[php]

Contacts

Your Name:

E-mail*:

Subject:

Message*:

Fields marked with an asterisk(*) are required!

[/php]

mail form
[php]<?php
if (isset($_POST[‘name’])) $name = $_POST[‘name’];
if (isset($_POST[‘email’])) $email = $_POST[‘email’];
if (isset($_POST[‘subject’])) $subject = $_POST[‘subject’];
if (isset($_POST[‘message’])) $message = $_POST[‘message’];

$mail_to = "[email protected]";//Change this email to yours

$message = stripslashes($message);
$headers = “From: “.$name.” <”.$email.">\r\n";
$headers .= “Reply-To: “.$email.”\r\n”;
$headers .= “X-Mailer: PHPMailer”."\r\n";
$headers .= "Content-Type: text/plain; charset=“utf-8"”;
if (mail($mail_to, $subject, $message, $headers)) {
?>

Ok

Thanks!
Your message has been successfully sent!

<?php } else { ?>

Error

Error Occured.
Try again later.

<?php } ?>[/php]

js

/*----------Contact Form ----------*/ $("#contact_form").submit(function(){ sendMessage() return false; }) function validateEmail() { var emailInput = $("#contact_email") var email = emailInput.attr("value"); var re = /^\w+([\.-]?\w+)*@(((([a-z0-9]{2,})|([a-z0-9][-][a-z0-9]+))[\.][a-z0-9])|([a-z0-9]+[-]?))+[a-z0-9]+\.([a-z]{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|mobi|travel))$/i; if(re.test(email)) emailInput.removeClass("required"); else emailInput.addClass("required"); } function validateMessage() { message = $("textarea#contact_message").val(); m_length = ($.trim(message)).length; if (m_length < 10) $("textarea#contact_message").addClass("required"); else $("textarea#contact_message").removeClass("required"); } function sendMessage() { validateEmail(); validateMessage(); if (!$("#contact_email,#contact_message").hasClass("required")) { var name = $("#contact_name").attr("value"); var email = $("#contact_email").attr("value"); var subject = $("#contact_subject").attr("value"); var message= $("#contact_message").val().replace(/<\/?[^>]+>/gi, ''); $(".contact-form").fadeOut(600,function(){ $(".contact-status").html('<img class="ajax-loader" src="'+ajaxLoader.src+'" width="'+ajaxLoader.width+'" height="'+ajaxLoader.height+'" />').show() }) jQuery.post("scripts/send_message.php",{name : name, email : email, subject : subject, message : message}, function (status) { $("#contact_form").fadeTo(300,0,function(){ $(this).css({"visibility":"hidden"}); $(".status_message").html(status).fadeIn(300) }); } ); } }

New form
[php]
Name



Email



Message


<div id="wrap" align="center">
	<img src="get_captcha.php" alt="" id="captcha" />

	<br clear="all" />
	<input name="code" type="text" id="code">
</div>
<img src="refresh.jpg" width="25" alt="" id="refresh" />

<br clear="all" /><br clear="all" />
<label>&nbsp;</label>
<input value="Send" type="submit" id="Send">
[/php]

mail form
[php] <?php
session_start();

//include('dbcon.php');

if(@$_REQUEST['code'] || @strtolower($_REQUEST['code']) == strtolower($_SESSION['random_number']))
{
	
	// insert your name , email and text message to your table in db
	
	echo 1;// submitted 
	
}
else
{
	echo 0; // invalid code
}
?>[/php]

js

[code]$(document).ready(function() {

 $('#Send').click(function() {  
 	
		// name validation
		
		var nameVal = $("#name").val();
		if(nameVal == '') {
			
			$("#name_error").html('');
			$("#name").after('<label class="error" id="name_error">Please enter your name.</label>');
			return false
		}
		else
		{
			$("#name_error").html('');
		}
		
		/// email validation
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var emailaddressVal = $("#email").val();
		
		if(emailaddressVal == '') {
			$("#email_error").html('');
			$("#email").after('<label class="error" id="email_error">Please enter your email address.</label>');
			return false
		}
		else if(!emailReg.test(emailaddressVal)) {
			$("#email_error").html('');
			$("#email").after('<label class="error" id="email_error">Enter a valid email address.</label>');
			return false
		 
		}
		else
		{
			$("#email_error").html('');
		}
	
		$.post("post.php?"+$("#MYFORM").serialize(), {
	
		}, function(response){
		
		if(response==1)
		{
			$("#after_submit").html('');
			$("#Send").after('<label class="success" id="after_submit">Your message has been submitted.</label>');
			change_captcha();
			clear_form();
		}
		else
		{
			$("#after_submit").html('');
			$("#Send").after('<label class="error" id="after_submit">Error ! invalid captcha code .</label>');
		}
		
		
	});
			
	return false;
 });
 
 // refresh captcha
 $('img#refresh').click(function() {  
		
		change_captcha();
 });
 
 function change_captcha()
 {
 	document.getElementById('captcha').src="get_captcha.php?rnd=" + Math.random();
 }
 
 function clear_form()
 {
 	$("#name").val('');
	$("#email").val('');
	$("#message").val('');
 }

});
[/code]

get captcha

[php]<?php
session_start();

$string = ‘’;

for ($i = 0; $i < 5; $i++) {
$string .= chr(rand(97, 122));
}

$_SESSION[‘random_number’] = $string;

$dir = ‘fonts/’;

$image = imagecreatetruecolor(165, 50);

// random number 1 or 2
$num = rand(1,2);
if($num==1)
{
$font = “Capture it 2.ttf”; // font style
}
else
{
$font = “Molot.otf”;// font style
}

// random number 1 or 2
$num2 = rand(1,2);
if($num2==1)
{
$color = imagecolorallocate($image, 113, 193, 217);// color
}
else
{
$color = imagecolorallocate($image, 163, 197, 82);// color
}

$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image,0,0,399,99,$white);

imagettftext ($image, 30, 0, 10, 40, $color, $dir.$font, $_SESSION[‘random_number’]);

header(“Content-type: image/png”);
imagepng($image);

?>[/php]

any help appreciated I have tried but I keep getting captcha img wrong yet still revive a blank email?? and I know the text is correct

i had the same problem but then found this tutorial
http://www.html-form-guide.com/contact-form/html-contact-form-captcha.html

Sponsor our Newsletter | Privacy Policy | Terms of Service