Forums

Chronoform v4 and ReCaptcha v2

napster89 09 Feb, 2021
Hi everyone,
i need your help, i have chronoform v4 and i would not like to update it ...
In my form in the "Preview" field I entered "Custom Item" which contains:
<script src="https://www.google.com/recaptcha/api.js?hl=it" async defer></script><body><form action="?" method="POST"><div class="g-recaptcha" data-sitekey="MYWEBSITEKEY"></div><br/><b>*Dati obbligatori</b><br/><br/><input name="submit" class="" value="Invia" type="submit" /></form></body></html>
And in "Event" in "On Submit" the "Custom server side validation" function which contains:
<?php// Checks if form has been submittedif ($_SERVER['REQUEST_METHOD'] == 'POST') {    function post_captcha($user_response) {        $fields_string = '';        $fields = array(            'secret' => 'MYSECRETKEY',            'response' => $user_response        );        foreach($fields as $key=>$value)        $fields_string .= $key . '=' . $value . '&';        $fields_string = rtrim($fields_string, '&');        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');        curl_setopt($ch, CURLOPT_POST, count($fields));        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);        $result = curl_exec($ch);        curl_close($ch);        return json_decode($result, true);    }    // Call the function post_captcha    $res = post_captcha($_POST['g-recaptcha-response']);    if (!$res['success']) {        // What happens when the CAPTCHA wasn't checked        echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';    } else {        echo 'success';    }}?>
And "In case of error" I entered "Event Loop".
But it does not work.
Can you help me please?
napster89 10 Feb, 2021
I have change PHP code in:
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => 'MYSECRETKEY',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

$result = curl_exec($ch);
curl_close($ch);

return json_decode($result, true);
}

// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);

if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
return 'fail';
} else {
echo 'success';
}
}
?>
Where the modify is:
if (!$res['success']) {        // What happens when the CAPTCHA wasn't checked        return 'fail';    } else {        echo 'success';    }
But it not work
napster89 10 Feb, 2021
1 Likes
Now i have change it in:
    if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
return false;
} else {
echo 'success';
}
And now it works, but i would like to see an error message, how can i do?
Thanks
Max_admin 14 Feb, 2021
it's difficult to support v4 today, it's better if you install v7 on the same website and build that form there!

but basically, you can echo a styled message HTML!
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
napster89 25 Feb, 2021
Answer
I have solved my problem:
1. In Preview i have add 2 Costum Element, in the first i insert:
<script>
function recaptchaCallback() {
jQuery('#submit').prop('disabled', false);
}
function recaptchaError() {
jQuery('#error').show();
}
</script>
In the Second:
<script src="https://www.google.com/recaptcha/api.js?hl=it" async defer></script>
<body>
<form action="?" method="POST">
<div class="g-recaptcha" data-callback="recaptchaCallback" data-error-callback="recaptchaError" data-sitekey="PUBLIC_KEY"></div>
<p id="error" style="color:red; display:none;"><b>Error Message!</b></p>
<br/>
<input id="submit" type="submit" name="submit" class="" value="Invia" disabled>
</form>
</body>
</html>
2.In Events i have insert in ON SUBMIT the Costum Server Side Validation, and OnFail the Event Loop

3. In the Costum Server Side Validation i have insert the following code:
<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => 'PRIVATE_KEY',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

$result = curl_exec($ch);
curl_close($ch);

return json_decode($result, true);
}

// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);

if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
return false;
} else {
return true;
}
}
?>
I hope it will be of help to anyone who is having the same problem as me
You need to login to be able to post a reply.