I have an issue regarding hCaptcha. I'm trying to set it on a form with two buttons firing two different actions.
Problem is, when I solve the second button it works, but when I solve the first one it doesn't, sends me a missing-input-response error.
So I understand that hCaptcha is looking for key/value pairs in the form, and the second button is overwriting the first one, so that when I solve the first the token isn't stored and therefore not handled to back end.
Here's a code sample :
<?php
$captcha = 0;
if(isset($_POST['h-captcha-response']))
{
$data = array(
'secret' => "my_secret",
'response' => $_POST['h-captcha-response']
);
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://hcaptcha.com/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
var_dump($response);
$responseData = json_decode($response);
if($responseData->success)
$captcha = 1;
}
else
$captcha = 1;
if($captcha == 1)
{
?>
<html>
<head>
</head>
<body>
<form action="#" method=POST id="form_">
<textarea name=input_ rows="1" cols="40" id="input"></textarea><br/>
<input type=submit name=perform1 value=perform1 id="submit1" class="h-captcha" data-sitekey="mykey" data-callback="onSubmit"/>
<input type=submit name=perform2 value=perform2 id="submit2" class="h-captcha" data-sitekey="mykey" data-callback="onSubmit"/>
<br/>
</form>
<script type="text/javascript">
function onSubmit(token) {
document.getElementById('form_').submit();
}
</script>
</body>
</html>
<?php
}
?>
I tried various things, such as store the token in hidden input but it didn't work. Seems odd that it cannot handle two submit buttons. I know reCaptcha has the same issue but I found some solutions online for it. Here I cannot find anything.
If you have any idea of a solution.
Thank you :)