Using new Google reCAPTCHA with PHP and AJAX

In the tutorial, I will be using a working HTML contact form. We will submit it via AJAX (the page will not reload) and process the form values with PHP.

Google has released the new reCAPTCHA. They need just a single click to confirm they are not a robot. So, reCAPTCHA will protect your website from spam with better user experience. You can easily integrate Google reCAPTCHA in PHP script using ajax validation.

Header file Code

<script src="" async defer></script>
<script src=""></script>


<form action="" method="POST" onsubmit="return validateForm(this);">
	<input type="text" required="" class="text" value="" placeholder="Your name" name="name" >
	<input type="email" required="" class="text" value="" placeholder="Email adress" name="email" >
	<textarea type="text" placeholder="Message..." required="" name="message"></textarea>
	<div class="g-recaptcha" data-sitekey=""></div>
	<input type="submit" name="submit" value="SUBMIT">

JavaScript / Jquery Code

function validateForm(form) {
    var flag = 0;
    var status = false;
	var captcha = grecaptcha.getResponse();
    if (captcha == '') {
		 flag = 1;
		alert("please Select CAPTCHA !!");
    if (flag == 1) 
        return status;
		   data: {captcha: captcha},
		   dataType: 'JSON',
				if(data.status == 1)
					status = true;
					alert('CAPTCHA Time out');
					status = false;
	    return status;


Call Ajax (Submit.php)

	$secret = '';
	$verifyResponse = file_get_contents(''.$secret.'&response='.$_POST['captcha']);
	$responseData = json_decode($verifyResponse);
	$json['success'] = $responseData->success;
	echo json_encode($json);

Leave a Reply

Your email address will not be published. Required fields are marked *