NK. NK. - 3 months ago 146
Ajax Question

How to create google reCAPTCHA v2.0 validation useing jQuery Validation Plugin and PHP

I want to create validation for recaptcha filed using jQuery Validation Plugin and also php with Session.

jQuery



I am useing (https://jqueryvalidation.org/) this jquery plugin for form validation and i do it. but i want to create validation for recaptcha filed using this plugin, but i can't create it.
Here is a validation for google recaptcha v1.0 using these plugin.
Please help me to create validation for google recaptcha v2.0 (filed) using this pulgin.

PHP



I am also want to create validation use $_SESSION and i am try to create it, in my (signup-process.php) page and i print the session message in my form page under the recaptcha filed. it's not working :(

======
Please help me to create validation for recaptcha filed using this both way.

HTML



<form action="signup-process.php" method="post" id="signupForm">
<input class="form-control" type="text" name="name" id="name">
<div class="g-recaptcha" data-sitekey="my-key"></div>

<!--Session validation message-->
/*<?php if(isset($_SESSION["ReC"]) && !empty($_SESSION["ReC"])){
echo $_SESSION["ReC"];
unset($_SESSION["ReC"]); }?> */

<input class="btn btn-block" type="submit" value="Sign Up" name="submitted">
</form>


JS



$("#signupForm").validate({
rules: {
name: "required",
},
messages: {
name: "Please enter your name",
}
});


signup-process.php



if(isset($_POST['g-recaptcha-response'])&& !empty($_POST['g-recaptcha-response'])){

$secret = "my-key";
$ip = $_SERVER['REMOTE_ADDR'];
$captcha = $_POST['g-recaptcha-response'];
$rsp = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip$ip");

$arr = json_decode($rsp,TRUE);
if($arr['success'] == "true"){
echo 'Done';
}else{
echo 'SPam';
} else {
$_SESSION["ReC"]="Please select captcha";
header("location:signup.php");
}

Answer

@NK If you apply your php validation message On top of the form tag. it will works.

like this

 <!--Session validation message-->
 <?php if(isset($_SESSION["ReC"]) && !empty($_SESSION["ReC"])){
    echo $_SESSION["ReC"];
    unset($_SESSION["ReC"]); }
?>

<form action="signup-process.php" method="post" id="signupForm">
  <input class="form-control" type="text" name="name" id="name">
  <div class="g-recaptcha" data-sitekey="my-key"></div>
  <input class="btn btn-block" type="submit" value="Sign Up" name="submitted">
</form>
Comments