0

I have one PHP form with some fields and google ReCAPTCHA field and values store in the database table, But i want to make the google ReCAPTCHA field required. Code here:

<?PHP

if(isset($_POST['submit']))
    {
{
        $name=$_REQUEST['name'];
        $email=$_REQUEST['email'];
        $message=$_REQUEST['message'];
        $user_id=$_SESSION['id'];   
        $sql="insert into contact (name,email,message,user_id,status)
        values('$name','$email','$message','$user_id','1')";
        $qex=mysql_query($sql); 
        if(!$qex)
        {
            die("Contact information  is not Added".mysql_error());     
        }   
        $msgsec="Contact information is Added";
?>

form code:

<script src='https://www.google.com/recaptcha/api.js'></script>


<form id='contactus' method='post' >
        <input type='hidden' name='submit' id='submit' value='1'/>
        <label><h2>Your Name <strong style="color:red">*</strong></h2></label>
            <input type="text" class="form-control" required name="name" id="name" placeholder="Please enter you'r name"/>
        <label><h2>Your Email <strong style="color:red">*</strong></h2></label>
            <input type="email" class="form-control" required name="email" id="email" placeholder="Please enter you'r email address"/>
        <label><h2>Your Message <strong style="color:red">*</strong></h2></label>
            <textarea class="form-control" required name="message" id="message" placeholder="Please type you'r message here"></textarea>
        <br  />
    <div class='container'>
        <div class="g-recaptcha" data-sitekey="6LevWB0UAAAAAEPIUh40HptW3PxfYFqjvz2Wa05D"></div>
    </div>
    <div class='container'>
        <input type="submit" name="submit" class="btn btn-primary" value="SEND MESSAGE">
    </div>  
  </form>

as of now the Google ReCAPTCHA option is not required. the form details always stored with submit button wheather i click on captcha or not. i want to make Google ReCAPTCHA required. please check my code and let me know. what i am missing.

Thanks and Regards. Ankit

Ankit
  • 59
  • 2
  • 7
  • Ajax would be an effective solution here. – Rotimi Apr 18 '17 at 06:01
  • 2
    Possible duplicate of [Google ReCAPTCHA how to make required?](http://stackoverflow.com/questions/29612879/google-recaptcha-how-to-make-required) – Rohan Khude Apr 18 '17 at 06:01
  • Possible duplicate of [new google recaptcha with checkbox server side php](http://stackoverflow.com/questions/27274157/new-google-recaptcha-with-checkbox-server-side-php) – Mittul At TechnoBrave Apr 18 '17 at 06:02
  • sir this form will be filled by clients & google RECAPTCHA will also be selected by the clients but which is accessible by the google ReCAPTCHA & all the form values will store on the DB table. – Ankit Apr 18 '17 at 06:03
  • So you did not find any similar question on SO ? – Hmmm Apr 18 '17 at 06:08
  • i find lots of questions regarding this but not get exact solution from there. – Ankit Apr 18 '17 at 06:10
  • What you mean by *exact* ? – Mittul At TechnoBrave Apr 18 '17 at 06:11
  • You just want the Google ReCAPTCHA to be required right? or anything else ? – Hmmm Apr 18 '17 at 06:11
  • sir i mean i used that codes but not working – Ankit Apr 18 '17 at 06:11
  • yes i want Google ReCAPTCHA to be required & if clients will try to submit the form without selecting ReCAPTCHA then the form should not store values & display like please solve captcha – Ankit Apr 18 '17 at 06:13
  • 1
    Very Nice ... Just check the links added by Rohan or Mittul. Do add it in your code, I am sure you will be able to solve it in few mins if you try! – Hmmm Apr 18 '17 at 06:14
  • @Hmmm exactly. Its already implemented. It should be fine. – Mittul At TechnoBrave Apr 18 '17 at 06:15
  • sorry sir i tried mittuls code & only get this "Please check the the captcha form." not even my form. sir can you please show me correct code by changing in my shared code? Regards - Ankit – Ankit Apr 18 '17 at 06:27

2 Answers2

4

Here is google captcha with validation

 <html>
<script src='https://www.google.com/recaptcha/api.js'></script>
<?PHP

/*Site Key and secret key is different thing so change it with ur keys */
$errMsg ="";
if(isset($_POST['submit']))
{
        if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
        //your site secret key

        $secret = '*********************';
        //get verify response data
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        print_r($responseData);

        $name = !empty($_POST['name'])?$_POST['name']:'';
        $email = !empty($_POST['email'])?$_POST['email']:'';
        $message = !empty($_POST['message'])?$_POST['message']:'';
        $user_id=$_SESSION['id'];   
        if($responseData->success):
            //contact form submission code
            $sql="insert into contact (name,email,message,user_id,status)
            values('$name','$email','$message','$user_id','1')";
            $qex=mysql_query($sql); 
            if(!$qex)
            {
                die("Contact information  is not Added".mysql_error());     
            }   
            $errMsg="Contact information is Added";

        else:
            $errMsg = 'Robot verification failed, please try again.';
        endif;
    else:
        $errMsg = 'Please click on the reCAPTCHA box.';
    endif;

}
echo $errMsg ;
?>
<body>
<form id='contactus' method='post' >
        <input type='hidden' name='submit' id='submit' value='1'/>
        <label><h2>Your Name <strong style="color:red">*</strong></h2></label>
            <input type="text" class="form-control" required name="name" id="name" placeholder="Please enter you'r name"/>
        <label><h2>Your Email <strong style="color:red">*</strong></h2></label>
            <input type="email" class="form-control" required name="email" id="email" placeholder="Please enter you'r email address"/>
        <label><h2>Your Message <strong style="color:red">*</strong></h2></label>
            <textarea class="form-control" required name="message" id="message" placeholder="Please type you'r message here"></textarea>
        <br  />
    <div class='container'>
        <div class="g-recaptcha" data-sitekey="*********************"></div>
    </div>
    <div class='container'>
        <input type="submit" name="submit" class="btn btn-primary" value="SEND MESSAGE">
    </div>  
  </form>
</body>  
</html>
Sunny Khatri
  • 537
  • 7
  • 15
  • sir i already checked try this before but with this no way shown to store the form values in the db table – Ankit Apr 18 '17 at 06:09
  • You have to store value on success condition. and you have problem on checking google captcha or data saving? – Sunny Khatri Apr 18 '17 at 06:11
  • sir on click submit my values are stored in the DB table the issue is weather i solve captcha or not the form always store values so i just want to make captcha to be required. so that clients must solve captcha to store the form filled values, except they get error like -- "please solve captcha". – Ankit Apr 18 '17 at 06:16
  • Hello ankit i update answer u can check it with ur secret key and site key. – Sunny Khatri Apr 18 '17 at 07:06
0

in your php file add this

if(isset($_POST['submit']))
{
 if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    $user_id=$_SESSION['id']; 

    $sql="insert into contact (name,email,message,user_id,status)
    values('$name','$email','$message','$user_id','1')";
    $qex=mysql_query($sql); 
    if(!$qex)
    {
        die("Contact information  is not Added".mysql_error());     
    }   
    $msgsec="Contact information is Added";
      }
    else
    {
    //your message for select recaptcha or required
    }
  }