renzorthered
New Coder
Hi guys. I'm trying to set up a Recaptcha V3 challenge on an html contact form, but I don't know enough about PHP to add to the corresponding PHP form to only proceed with the required fields check and email submission if Recaptcha passes. Can anyone tell me where in my existing code to confirm a successful recaptcha, and what to put in there? The clientside stuff is so simple, but the serverside is Greek to me. Or if there's an easier way that doesn't even involve the PHP?
Trying to alter the PHP to effectively say "If Recaptcha challenge fails or equals zero (or whatever), then say that there was an error and go back." is stumping me. And I suppose I must put something in with the serverside (secret) key to make it work. Don't know.
The HTML Form:
The PHP File (Don't know where to put a challenge success confirmation or what to insert.)
Trying to alter the PHP to effectively say "If Recaptcha challenge fails or equals zero (or whatever), then say that there was an error and go back." is stumping me. And I suppose I must put something in with the serverside (secret) key to make it work. Don't know.
The HTML Form:
HTML:
<form name="contactform" method="post" action="(website url)/test_form.php">
<center><font size="+1"><strong>Contact us for a FREE ESTIMATE</strong> or with any Questions / Comments</font>
<table width="650" border="5" bordercolor="#333333" cellspacing="5" bgcolor="#003366" cellpadding="5">
<tr>
<td width="120" bgcolor="#FFFFFF"><label for="first_name">First Name * </label></td>
<td width="150"><input type="text" name="first_name" maxlength="100" size="26"></td>
<td width="120" bgcolor="#FFFFFF"><label for="last_name">Last Name * </label></td>
<td width="150"><input type="text" name="last_name" maxlength="100" size="26"></td>
</tr>
<tr>
<td width="120" bgcolor="#FFFFFF"><label for="telephone">Phone number * </label></td>
<td width="150"><input type="text" name="telephone" maxlength="100" size="26"></td>
<td width="120" bgcolor="#FFFFFF"><label for="altphone">Alternative Phone </label></td>
<td width="150"><input type="text" name="altphone" maxlength="100" size="26"></td>
</tr>
<tr>
<td width="120" bgcolor="#FFFFFF"><label for="email">Email * </label></td>
<td width="150"><input type="text" name="email" maxlength="200" size="26"></td>
<td width="120" bgcolor="#FFFFFF"><label for="besttime">Best time to call? </label></td>
<td width="150"><input type="text" name="besttime" maxlength="100" size="26"></td>
</tr>
<tr>
<td colspan="4" bgcolor="#FFFFFF"><center><label for="comments">Your message. If asking about a free estimate, <br />please tell us about what sort of exterior services you're looking for *</label></center></td></tr>
<tr><td colspan="4" bgcolor="#FFFFFF"><center><textarea name="comments" maxlength="2000" cols="90" rows="6"></textarea></center></td>
</tr>
<tr>
<td colspan="4"><center>
<button class="g-recaptcha"
data-sitekey="(client side key)"
data-callback='onSubmit'
data-action='submit'>Submit Your Message</button>
<!--div class="g-recaptcha" data-sitekey="(client side key)"></div-->
<!--input name="submit" type="submit" value="Submit Your Message" /--></center></td>
</tr>
</table></center>
</form>
The PHP File (Don't know where to put a challenge success confirmation or what to insert.)
PHP:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "(my email address)";
$email_subject = "** Test FORM - Customer Contact";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please GO 'BACK' and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Alt Telephone: ".clean_string($altphone)."\n";
$email_message .= "Best Time to Call: ".clean_string($besttime)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
/* Redirect browser */
?>
<!-- include your own success html here -->
<center>
(The 'thanks for contacting' code I have)
</center>
<?php
}
?>
Last edited: