Google reCaptcha v2 (Checkbox) Verification - php

I have a contact form which is working fine. I have Integrate Google ReCaptcha v2 with it and it is showing below my contact form but form will be submitted either user checked reCaptcha or not. I want to verify it that form only goes submit if user has checked google ReCaptcha otherwise it shows a message to the user to check it.
Here is my HTML Form (contact.html):
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer>
</script>
</head>
<body>
<form action="contact_us.php" method="post">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Your
Name *" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email *" required>
</div>
<div class="form-group">
<input type="text" name="phone" class="form-control" placeholder="Contact Number">
</div>
<div class="form-group">
<textarea class="form-control textarea" name="message" placeholder="Message"></textarea>
</div>
<div class="form-group">
<div class="g-recaptcha" data sitekey="have_enter_my_site_key_here"></div>
</div>
<div class="form-group">
<input type="submit" name="submit" class="form-control btn-
submit" Value="Send">
</div>
</form>
</body>
</html>
Here is PHP code as (contact_us.php):
<?php
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from ='';
$email_subject = "Contact Form";
$email_body = "User Name: $name \n".
"User Email: $email \n".
"Phone Number: $phone \n".
"User Message: \n $message \n";
$to = "my_Email_here";
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $email \r\n";
$result = mail($to,$email_subject,$email_body,$headers);
if($result) {
echo "Sent Successfully";
}
else{
echo "Something Went wrong. Please try again";
}
?>
Help me how should I verify google ReCaptcha with my HTML form.

<?php
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
// Your site secret key obtained from Google
$secret = '#####################################';
$grResponse = $_POST['g-recaptcha-response'];
// Verify with Google Servers
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $grResponse);
$responseData = json_decode($verifyResponse);
if ($responseData->success) {
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from = '';
$email_subject = "Contact Form";
$email_body = "User Name: $name \n" . "User Email: $email \n" . "Phone Number: $phone \n" . "User Message: \n $message \n";
$to = "my_Email_here";
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $email \r\n";
$result = mail($to, $email_subject, $email_body, $headers);
if ($result) {
echo "Sent Successfully";
}
else {
echo "Something Went wrong. Please try again";
}
} else {
echo "################################";
}
} else {
echo "################################";
}
?>
Please check below code:

Related

creating a contact page in HTML and PHP can getting a return error when testing (HTML 500)

So I am rather new to coding in these languages, I am getting a good understanding of how to write in them but not how to troubleshoot well. I went on and found some videos and read some sites to understand how to get this to work, and got this
HTML
<form id="Contact-form" method="post" action="contactform.php">
<input type="text" name="name" placeholder="Your Name" class="form-control" required>
<input type="text" name="Mail" placeholder="Your E-mail" class="form-control" required>
<input type="text" name="name" placeholder="Subject" class="form-control" required>
<textarea name="message" placeholder="Message" rows="7" class="form-control" required></textarea>
<button class="form-control submit" type="submit" name="sumbit"> Send message</button>
The PHP
<?php
if(isset($_POST['submit'])){{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email"
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend")
}
I have it live and attempted to test send myself an email and I get a basic error (HTML Error 500) and no email, it changes the URL to "contactform.php" as expected. I followed the tutorials to the T, what am I doing wrong!
Thank you for the help in advance
Please change the codes from
<?php
if(isset($_POST['submit'])){{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email"
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend")
}
to
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend");
}

Why can't I see the email address from contact form?

I receive an email but it will read from unknown. Everything else seems to work. Here is a link to the page link I don't know much about php.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mailTo = "contact#podmtg.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.html?mailsend");
}
?>
<form action="contactform.php" method="post" onsubmit="return confirm('Are you sure you want to submit this form?');">
<input type="text" name="name" placeholder="Name" required="required">
<input type="text" name="email" placeholder="Email" required="required">
<textarea name="message" placeholder="Write message here..." required="required"></textarea>
<button type="submit" name="submit">SUBMIT</button>
</form>
In this you have mention this `$headers = "From: ".$mailFrom; but not $mailFrom found. Use $email in place of $mailFrom.
I changed the code. You guys were right. Thank you!
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];
$mailTo = "contact#podmtg.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$message;
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsend");
}
?>

PHP email and url validation

So I've made myself a little contact form with php, css, and html. But when I try to add a email validation it still sends the email and doesn't change the style of the input to red (Like I would like it to). Another issue I'm having is the button redirecting to the top of the page (which I do not want it to do). Last I can I make the input keep the text rather than remove it once submitted
HTML:
<div id="contact">
<div class="container">
<form id="contact-form" method="post">
<h1>Contact Form</h1>
<fieldset>
<input placeholder="Your Name" type="text" name="name" required>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" type="email" name="email" id="email-input" required>
</fieldset>
<fieldset>
<input placeholder="Your Phone Number (optional)" type="tel" name="phone" required>
</fieldset>
<fieldset>
<input placeholder="Your Web Site (optional)" type="url" name="site" required>
</fieldset>
<fieldset>
<textarea placeholder="Type your message here...." name="message" required></textarea>
</fieldset>
<fieldset>
<button type="submit" id="contact-submit" name="submit">Submit</button>
</fieldset>
</form>
</div>
</div>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style";
}
?>
Try this for email validation in php
<?php
if (isset($_POST) && !empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style>";
}
}
?>
As per our chat conversation. I am adding jquery ajax function according to your form requirement.
You need to create new file email.php and put your php code into this separate php file
<script>
var url = 'email.php';
$.ajax({
url : url,
type : "POST",
dataType : "JSON",
data : $('#contact-form').serialize(),
success : function(response) {
if (response.error == 0) { // success
$('#contact-form')[0].reset();
alert('Form submitted successfully. We will contact you asap.');
} else { // error
$('#email-input').css('color', 'red');//in case of email error
alert('ERROR MESSAGE');//form is invalid
}
}
})
</script>
To handle JSON request you need to send JSON object in response. So change you php code snippet like this:
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
exit(json_encode(array('error' => 0)));
} else {
exit(json_encode(array('error' => 1)));
}

How to apply Google reCAPTCHA (2.0) to my current HTML/PHP contact form

I am trying to apply Google reCAPTCHA to my HTML/PHP contact form. The reCAPTCHA appears on the page and works, but I cannot get it to work with the form.
Can somebody please tell me how to add the appropiate PHP code to integrate the recaptcha into my contact form?
<form action="submit.php" method="POST">
<input class="textbox" type="text" name="fullname" placeholder="Your Name">
<input class="textbox" type="email" name="email" placeholder="E-Mail">
<input class="textbox" type="tel" name="telephone" placeholder="Telephone">
<input class="textbox" type="text" name="business" placeholder="Organization">
<textarea class="comments_box" name="visitor_message" placeholder="Message"></textarea>
<div class="g-recaptcha" data-sitekey="6LddnxAUAAAAAAi6jmmUcX8pPMfSELRgpNUAI2Ra"></div>
<br>
<input class="submitBtn" type="submit" value="SUBMIT">
</form>
<?php
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['telephone']) && isset($_POST['business']) && isset($_POST['visitor_message'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$telephone= $_POST['telephone'];
$business = $_POST['business'];
$visitor_message = $_POST['visitor_message'];
if (!empty($fullname) && !empty($email) && !empty($telephone) && !empty($business) && !empty($visitor_message)) {
$to = 'acramirez38#gmail.com';
$subject = 'Trucker Radio Talk Visitor Message';
$body = "Full Name: " . $fullname ."\n". "E-Mail: " . $email ."\n". "Telephone: " . $telephone ."\n". "Business: " . $business ."\n". "Comments: " . $visitor_message;
$headers = 'From: ' .$email;
if (mail($to, $subject, $body, $headers))
echo 'Thank you for contacting us! Your message has been successfully delivered.';
} else {
echo 'All fields are required! Please return to the previous page and revise.';
}
}
?>

sending form info to email

I have a form that sends info to php doc. When testing, I submit and array displays but won't go to email address.
<?php require_once("/includes/fbcheader.php");
?>
<div class="comments">
<h3> We Welcome All Comments </h3>
<form action="/php/contact-send.php" method="post"
id="contact-form" class="form">
<p class="input-block">
<label for="form-name" >Name</label>
<input type="text" value name="name" id="form-name"
autofocus placeholder="Please enter name" required>
</p>
<p class="input-block">
<label for="form-email" >Email</label>
<input type="email" value name="email" id="form-email"
required placeholder="Please enter E-Address">
<input type="hidden" value name="age" id="age">
</p>
<p class="input-block">
<label for="form-subject" >Subject</label>
<input type="text" value name="subject" id="form-subject"
required placeholder="Please enter subject">
</p>
<p class="textarea-block">
<label for="form-message">Comment</label>
<textarea name="message" id="form-message"
cols="70" rows="10"></textarea>
</p>
<div class="clear"></div>
<input type="hidden" name="firstname" id="firstname">
<input type="submit" value="Send" id="form-submit">
<p class="hide" id="response"></p>
<div class="hide">
<label for="spam-check">Do not fill out this field</label>
<input name="spam-check" type="text" value id="spam-check">
</div>
</form>
</div>
</body>
</html>
This is the sending php execute code
<html>
<head>
<title>Contact-Send</title>
</head>
<body>
<?php
if(null!==($_POST["name"])) {
$name = $_POST["name"];
/* echo "Please enter 'Name'<br />"; */
} else {
$name = "";
}
if(null!==($email = $_POST["email"])) {
$email = $_POST["email"];
/* echo "Please enter 'E_Mail'<br />"; */
} else {
$email = "";
}
if(null!==($email = $_POST["subject"])) {
$form_email = $_POST["subject"];
/* echo "Please enter 'Subject'<br />"; */
} else {
$subject = "";
}
if(null!==($message = $_POST["message"])) {
$message = $_POST["message"];
/* echo "Please enter your message<br />"; */
} else {
$message = "";
}
/*echo "{$name}, {$email}, {$message}";*/
$email_from = '$Email';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "me#myaddress.com";
?>
<br>
<?php
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $form_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("thank-you.html");
?>
</body>
</html>
On submit I simply get the array displaying the content of the input fields
A quick look at your PHP code I see the following:
This: if(null!==($email = $_POST["email"])) will fail because you are running a check against nothing. it should be: if(null!==($_POST["email"])).
You have an undefined variable here: $email_from = '$Email'; this will simply output "$Email". It should be $email_from = $email; (as per your code). However you are simply re-assigning the variable here which you dont really need to be doing at all.
Your concatenating the $email_body and $to address.
You header("thank-you.html"); will fail because as per your code, output has already started here <?php (line 6).
You should be aiming for something closer to this for your contact-send.php:
<?php
if(null!==($_POST["name"])) {
$name = $_POST["name"];
/* echo "Please enter 'Name'<br />"; */
} else {
$name = "";
}
if(null!==($_POST["email"])) {
$email = $_POST["email"];
/* echo "Please enter 'E_Mail'<br />"; */
} else {
$email = "";
}
if(null!==($_POST["subject"])) {
$form_email = $_POST["subject"];
/* echo "Please enter 'Subject'<br />"; */
} else {
$subject = "";
}
if(null!==($_POST["message"])) {
$message = $_POST["message"];
/* echo "Please enter your message<br />"; */
} else {
$message = "";
}
/*echo "{$name}, {$email}, {$message}";*/
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message";
$to = "me#myaddress.com";
$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: thank-you.html");
?>
Remember to set $to to your email address!

Categories