Data entered via Email Contact Form using g-recaptcha v.3 to create hidden token which is passed to form.php is not being displayed within emails received from local server. Can you help?
HTML Contact Form
<!-- Contact Section -->
<div class="w3-container w3-padding-32 w3-card w3-light-grey " id="contact">
<h3 class="w3-border-bottom w3-border-light-grey w3-padding-16">Contact</h3><hr>
<p>Lets get in touch and talk about your next project.</p>
<form action="form.php " method="post" target="_blank" >
<input class="w3-input w3-border w3-text-red" type="text" placeholder="Enter Your First Name: ex. Mark (with no spaces, punctuations or symbols)" name="fname" maxlength="32" pattern="[A-Za-z]{1,32}" required><br>
<input class="w3-input w3-border w3-text-red" type="text" placeholder="Enter Your Last Name: ex. Wilson (with no spaces, punctuations or symbols)" name="lname" maxlength="32" pattern="[A-Za-z]{1,32}" required>
<input class="w3-input w3-section w3-border w3-text-red" type="text" placeholder="Enter Your Email: ex. yourname@domainname.com" name="email" pattern="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" required>
<input class="w3-input w3-section w3-border w3-text-red" placeholder="Enter Your Phone Number: ex.(112)112-1112 or 1121121112" name="phone" pattern="^(\s*)?(\+)?([- _():=+]?\d[- _():=+]?){10,14}(\s*)?$" required>
<textarea class="w3-input w3-section w3-border w3-text-red" placeholder="Please Place Your Comment Here" name="comment" rows="5" required></textarea>
<i class="fa fa-paper-plane-o"></i>
<input type="submit" name="submit" value="SEND MESSAGE" class="w3-text-black"><br><br>
</form>
<script>
// when form is submit
$('#comment_form').submit(function() {
// we stoped it
event.preventDefault();
var fname = $('#fname').val();
var lname = $('#lname').val();
var email = $('#email').val();
var phone = $('#phone').val();
var comment = $("#comment").val();
// needs for recaptacha ready
grecaptcha.ready(function() {
// do request for recaptcha token
// response is promise with passed token
grecaptcha.execute('My Site Key', {action: 'create_comment'}).then(function(token) {
// add token to form
$('#comment_form').prepend('<input type="hidden" name="g-recaptcha-response" value="' + token + '">');
$.post("form.php",{fname: fname, lname: lname, email: email, phone: phone,comment: comment, token: token}, function(result) {
console.log(result);
if(result.success) {
alert('Thanks for posting comment.')
} else {
alert('You have been identified as a spammer. Your submitted comments have been blocked.')
}
});
});;
});
});
</script>
</div>
<div class = "w3-display-container w3-content w3-wide w3-text-black w3-center w3-dark-blue w3-tiny" style="max-width:1300px;">
<a>"Section 6"</a>
</div>
</div>
<!-- End page content -->
form.php
<?php
$fname; $lname;$email;$comment;$captcha;
$fname = filter_input(INPUT_POST, 'fname', FILTER_SANITIZE_STRING);
$lname = filter_input(INPUT_POST, 'lname', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$phone = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_STRING);
$comment = filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_STRING);
$captcha = filter_input(INPUT_POST, 'token', FILTER_SANITIZE_STRING);
if(!$captcha){
echo '<h2>Please re-check your information entered on the contact form.</h2>';
exit;
}
$secretKey = "My Secrete Key";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array('secret' => $secretKey, 'response' => $captcha);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$responseKeys = json_decode($response,true);
header('Content-type: application/json');
if($responseKeys["success"]) {
echo json_encode(array('success' => 'true'));
} else {
echo json_encode(array('success' => 'false'));
}
// Retrieve from HTML contact form section
$fname = trim(strip_tags($_POST['fname']));
$lname = trim(strip_tags($_POST['lname']));
$email = trim(strip_tags($_POST['email']));
$phone = trim(strip_tags($_POST['phone']));
$comment = htmlentities($_POST['comment']);
// Send to Default email, via the .PHP form
$subject = "New Comments submitted through the PSMPC Contact Form!";
$to = 'questions@pleasesavemypc.com';
$body = <<<HTML
First Name:  $fname <br> Last Name:  $lname <br> Email:  $email <br> Phone:  $phone<br> Comment:  $comment
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
//redirect afterwords, if needed
header('Location: thanks.html');
?>
Screen Shots
source https://stackoverflow.com/questions/68170293/can-you-review-my-website-email-contact-form-that-uses-php-and-g-recaptcha
Comments
Post a Comment