What's new

Welcome to xCrud Community - Data Management and extended PHP CRUD

Join us now to get access to all our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, and so, so much more. It's also quick and totally free, so what are you waiting for?

recaptcha xcrud

DaDo

Administrator
Staff member
Joined
Dec 1, 2021
Messages
108
Reaction score
23
Points
18
Hello, how can I integrate recaptcha with xcrud?



PHP:
//or you can use recaptcha as field_callback
$xcrud->field_callback('recaptcha_input','recaptha_function');

function recaptha_function($value, $field, $priimary_key, $list, $xcrud)
{
    
        
   return '<div class = "form-group col-lg-6">
       <div class="g-recaptcha" data-sitekey="MY_KEY"></div>
   </div>
   <input type = "button" id = "validate_captcha" value = "validate_captcha">';
}


//js example field_js callback
$('#validate_captcha').click(function() {
var captcha = "captcha";
  $.ajax({
    url: "captcha.php",
    method: "post",
    data:{captcha:captcha},
    success:function(data){
    if(data=='success'){
       $('form').submit();
       }
    }
    else{
       alert('captcha failed. try again');
    }
 });
});



//if you want you can use your javascript to integrate recaptcha
$xcrud->set_attrib('recaptcha_input',array('id'=>'yourcustomrecaptcha_id','class'=>'g-recaptcha','data-sitekey'=>'MY_KEY'));

//ajax file

<?php
if($_POST['captcha']){
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $privatekey = 'MY_SECRET_KEY';

        $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
        $data = json_decode($response);

        if($data->sucess==true){
            echo "success";
        }
        else{
            echo "failed";
        }
    }
?>
 
Top Bottom