Answer the question
In order to leave comments, you need to log in
How to fix google recaptcha error?
I am using google recaptcha v3.
Page code:
<?php
if(isset($_POST['send'])){
$site_key = '***********************************************';
$secret_key = '********************************************';
function getCaptcha($s){
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret_key."&response={$s}");
return json_decode($response);
}
}
?>
<!DOCTYPE html>
<html>
<head lang="uk">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Контакт</title>
<link rel="stylesheet" type="text/css" href="styles/general.css">
<link rel="stylesheet" type="text/css" href="styles/contact.css">
</head>
<body>
<div class="wraper">
<?php include 'pages/header.php'; ?>
<main>
<div class="wraper-boxes">
<div class="box">
<form method="post" action="">
<input type="text" placeholder="Ваше имя" name="name" title="Ваше ім'я" required autocomplete="off">
<input type="email" placeholder="Ваш email" name="email" title="Ваш email" required>
<textarea name="message" id="message" cols="30" rows="10" required placeholder="Повідомлення"></textarea>
<input type="text" id="g-recaptcha-response" name="g-recaptcha-response">
<input type="submit" name="send" class="send" value="Надіслати">
</form>
</div>
</div>
</main>
<?php include 'pages/footer.php'; ?>
</div>
<script src="https://www.google.com/recaptcha/api.js?render=<?php echo $site_key;?>"></script>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('<?php echo $site_key;?>', {action: 'submit'}).then(function(token) {
// Add your logic to submit to your backend server here.
console.log(token);
document.getElementById('g-recaptcha-response').value = token;
});
});
</script>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Your $site_key is only defined after submitting the form via POST
if(isset($_POST['send'])){
$site_key = '***********************************************';
...
<script>
grecaptcha.ready(function() {
grecaptcha.execute('<?php echo $site_key;?>', {action: 'submit'}).then(function(token) {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question