1

I am using PHP and AJAX together in my website to fetch data from a JSON URL and to display it on the webpage. When I use it without implementing recaptcha, it works fine but when I integrate Google's Recaptcha, the results get displayed only when the captcha puzzle is solved twice everytime. I am not sure where the bug actually lies and I even tried to implement a custom captcha and in that case also it is the same. Here is the code with recaptcha,

Captcha and Ajax code snippet :

<?php
if ($resp != null && $resp->success):  ?>

echo"hi";

<script>
$(document).ready(function(){

$("#submit").click(function(){

$.post("retrieve_train_between_stations.php", $("#get_train_running").serialize(),  function(response) {  
 $("#success").html(response);


});
return false;


});

});
</script>


<?php
else:
echo "Failed";

?>

Full code : http://pastebin.com/UynEiYng

Rizwan Ahmed
  • 919
  • 13
  • 19
  • Given your script, that is the expected behavior. – frz3993 Jan 01 '16 at 18:08
  • Where am I going wrong ? Is there any solution for the problem ? @frz3993 – Rizwan Ahmed Jan 01 '16 at 18:13
  • The first load of the script `$_POST` doesn't exist, so `$resp` will be false and your script part doesn't exist. On the first submit `$_POST` will be populated and `$resp` will become true, script part comes into existence. Only on the second submit will the script be effective. – frz3993 Jan 01 '16 at 18:23
  • My suggestion, make `retrieve_train_between_stations.php` check for the recaptcha, make the script always available (drop the if/else), prevent default behavior when submit is clicked. And also check if `isset($_POST)`. – frz3993 Jan 01 '16 at 18:25
  • @frz3993 : Oh Thank you so much for your suggestion I will try that and let you know ! Thank you once again ! – Rizwan Ahmed Jan 01 '16 at 18:34
  • @frz3993 : I tried what you said , but couldn't get to work either can you please help me out with a sample code ? – Rizwan Ahmed Jan 02 '16 at 05:36

1 Answers1

1

This part should be moved to retrieve_train_between_stations.php.

require_once "recaptchalib.php";
// your secret key
$secret = "My secret key"; 
// check secret key
$reCaptcha = new ReCaptcha($secret);

$resp = false;

if (isset($_POST["g-recaptcha-response"])) {
    $resp = $reCaptcha->verifyResponse(
        $_SERVER["REMOTE_ADDR"],
        $_POST["g-recaptcha-response"]
    );
}

if ($resp) {
    //display the record
} else {
    echo 'Recaptcha can not be verified.';
}

The if/else should be removed and prevent the default event for the script

<script>
$(document).ready(function(){

$("#submit").click(function(event){
    event.preventDefault();
    $.post("retrieve_train_between_stations.php", $("#get_train_running").serialize(),  function(response) {  
 $("#success").html(response);


        });
        return false;

    });

});
</script> 
frz3993
  • 1,595
  • 11
  • 13
  • I will try this one and will inform you whether it works or not. – Rizwan Ahmed Jan 02 '16 at 17:21
  • I did like this : http://pastebin.com/vBE5UF3G but now it is not giving me back results. – Rizwan Ahmed Jan 02 '16 at 17:25
  • 1
    I can see that you are trying to use `$resp` in a function context. You will need to pass it as argument to the function or use `$_GLOBAL` so it can be used inside a function context. Within the function context `$resp` will be undeclared. – frz3993 Jan 02 '16 at 17:37
  • Yes you are correct ! I didn't realize that ! Now I changed it and it works fine :) Thanks a lot and another solution also I found on stackoverflow, here is the alternative : http://stackoverflow.com/questions/27274157/new-google-recaptcha-with-checkbox-server-side-php Hope it helps :) – Rizwan Ahmed Jan 02 '16 at 17:57