I am still a novice and want to finalise my website but I cannot for the life of me figure out how to implement the re-captcha V3.
I have watched video after video but still not sure why its not working. When I add the g-recaptcha part to my Button class the form does not work. But once I remove it the form works.
Here is my code broken up in the different areas built as one of the videos guided:
JavaScript:
<script src="https://www.google.com/recaptcha/api.js"></script>
<script>
function onSubmit(token) {
document.getElementById("contact-form").submit();
}
</script>
HTML Form:
<div class="rainbow-contact-area rainbow-section-gapBottom" data-sal="slide-up" data-sal-duration="400" data-sal-delay="150">
<div class="container">
<div class="row mt--40 row--15">
<div class="col-lg-7">
<form class="contact-form-1 rainbow-dynamic-form" id="contact-form" method="POST" action="mail.php">
<div class="form-group">
<input type="text" name="contact-name" id="contact-name" placeholder="Your Name">
</div>
<div class="form-group">
<input type="text" name="contact-phone" id="contact-phone" placeholder="Phone Number">
</div>
<div class="form-group">
<input type="email" id="contact-email" name="contact-email" placeholder="Your Email">
</div>
<div class="form-group">
<input type="text" id="subject" name="subject" placeholder="Your Subject">
</div>
<div class="form-group">
<textarea name="contact-message" id="contact-message" placeholder="Your Message"></textarea>
</div>
<div class="form-group text-center">
<button name="submit" type="submit" id="submit" class="btn-default btn-large rainbow-btn g-recaptcha" data-sitekey="site_key" data-callback='onSubmit' data-action='submit'>Submit Now</button>
</div>
</form>
</div>
PHP:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'sercet_key';
$recaptcha_response = $_POST['g-recaptcha-response'];
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha, true);
$name = trim($_POST['contact-name']);
$phone = trim($_POST['contact-phone']);
$email = trim($_POST['contact-email']);
$message = trim($_POST['contact-message']);
if ($name == "") {
$msg['err'] = "n Name can not be empty!";
$msg['field'] = "contact-name";
$msg['code'] = FALSE;
} else if ($phone == "") {
$msg['err'] = "n Phone number can not be empty!";
$msg['field'] = "contact-phone";
$msg['code'] = FALSE;
} else if (!preg_match("/^[0-9 \-\+]{4,17}$/i", trim($phone))) {
$msg['err'] = "n Please put a valid phone number!";
$msg['field'] = "contact-phone";
$msg['code'] = FALSE;
} else if ($email == "") {
$msg['err'] = "n Email can not be empty!";
$msg['field'] = "contact-email";
$msg['code'] = FALSE;
} else if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$msg['err'] = "n Please put a valid email address!";
$msg['field'] = "contact-email";
$msg['code'] = FALSE;
} else if ($message == "") {
$msg['err'] = "n Message can not be empty!";
$msg['field'] = "contact-message";
$msg['code'] = FALSE;
} else {
$to = '[email protected]';
$subject = 'Website Form Submission';
$_message = '<html><head></head><body>';
$_message .= '<p>Name: ' . $name . '</p>';
$_message .= '<p>Message: ' . $phone . '</p>';
$_message .= '<p>Email: ' . $email . '</p>';
$_message .= '<p>Message: ' . $message . '</p>';
$_message .= '</body></html>';
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$headers .= 'From: <[email protected]' . "rn";
$headers .= 'cc: [email protected]' . "rn";
mail($to, $subject, $_message, $headers, '-f [email protected]');
$msg['success'] = "n Email has been sent successfully.";
$msg['code'] = TRUE;
}
}
echo json_encode($msg);?>
New contributor
DARKLiING is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1