I have a form that I want to add ReCaptcha to, however, somewhere along the line something isnt working correctly because my form will still submit whether or not reCaptcha is verified.
This is the form:
<?php if(isset($_GET[ 'CaptchaPass'])){ ?>
<div>Thank you! Your Form was Successfully Submitted</div>
<?php } ?>
<?php if(isset($_GET[ 'CaptchaFail'])){ ?>
<div>Captcha Error. Please verify that you are human!</div>
<?php } ?>
<form action="http://vmobileautoglass.com/php/func_contact.php">
<label>Name</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="name">
</div>
</div>
<label>Email
<span class="color-red">*</span>
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="email">
</div>
</div>
<label>Phone
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="phone">
</div>
</div>
<label>Message</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-8 col-md-offset-0">
<textarea rows="8" class="form-control" name="message"></textarea>
</div>
</div>
<div class="g-recaptcha" data-sitekey="MYSITEKEYFROMGOOGLE"></div>
<p>
<button type="submit" class="btn btn-primary" name="ContactButton">Send Message</button>
</p>
</form>
This goes at the very top of the page where the form is located:
<?php
if (isset($_POST['ContactButoon'])) {
$url = 'https://google.com/recaptcha/api/siteverify';
$privatekey = "MYPRIVATEKEYFROMGOOGLE";
$response = file_get_contents($url . "?secret=" . $privatekey . "&response=" . $_POST['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
$date = json_decode($response);
if (isset($data->success) AND $data->success == true) {
header('Location: contact.php?CaptchaPasss=True');
} else {
header('Location: contact.php?CaptchaFail=True');
}
}
?>
And this is the forms functionality:
// Receiving variables
#$pfw_ip = $_SERVER['REMOTE_ADDR'];
#$name = addslashes($_GET['name']);
#$email = addslashes($_GET['email']);
#$phone = addslashes($_GET['phone']);
#$message = addslashes($_GET['message']);
// Validation
if (strlen($name) == 0) {
header("Location: http://vmobileautoglass.com/php/err_name.php");
exit;
}
if (strlen($email) == 0) {
header("Location: http://vmobileautoglass.com/php/err_email.php");
exit;
}
if (strlen($message) == 0) {
header("Location: http://vmobileautoglass.com/php/err_message.php");
exit;
}
//Sending Email to form owner
$pfw_header = "From: $email\n"
. "Reply-To: $email\n";
$pfw_subject = "vMobile Contact Form";
$pfw_email_to = "vmobileag#gmail.com";
$pfw_message = "Visitor's IP: $pfw_ip\n"
. "name: $name\n"
. "email: $email\n"
. "phone: $phone\n"
. "message: $message\n";
#mail($pfw_email_to, $pfw_subject, $pfw_message, $pfw_header);
header("Location: http://vmobileautoglass.com/php/successform.php");
You have a typo:
$date = json_decode($response);
if(isset($data->success) AND $data->success==true){
$date should be $data.
Related
i have this contact form and php file to submit form, but in this, when i submit form it displays script alert msg about success and failure but i need to go back tp page, where form fields are still filled with info. instead of this i want that form should show alert on same page and form field shoud be cleared. how to do it? i am very beginner in php. dont know where to update code and how.ajax,json and PHP used in form
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['tel']) && isset($_POST['loc']) && isset($_POST['message'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$loc = $_POST['loc'];
$message = $_POST['message'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Kindly provide valid Email Address.";
} else {
$body = $name . "\n" . $email . "\n" . $tel . "\n" . $loc . "\n" . $message;
if (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email)) {
echo "Thanks for contacting us.";
}
else {
echo "<script>alert('Sorry!, there is a problem in sending email. Please call us on +61 452 534 200')</script>";
}
}
} else {
echo 'Message could not be sent.';
}
$curlx = curl_init();
curl_setopt($curlx, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($curlx, CURLOPT_HEADER, 0);
curl_setopt($curlx, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlx, CURLOPT_POST, 1);
$post_data =
[
'secret' => '6LfRXOEhAAAAAOWyRGugvbPdYOhkztlz1aMcEwKP', //<--- your reCaptcha secret key
'response' => $_POST['g-recaptcha-response']
];
curl_setopt($curlx, CURLOPT_POSTFIELDS, $post_data);
$resp = json_decode(curl_exec($curlx));
curl_close($curlx);
if ($resp->success)
{
echo "<script>alert('Thanks For connecting us..we will get back to you soon..')</script>";
} else
{
// failed
echo "<script>alert('Please verify you are human..')</script>";
exit;
}
?>
<div class="col-md-6 padding-15">
<div class="contact-form">
<!-- https://brickboys.com.au/contact.php -->
<form action ="https://brickboys.com.au/contact.php"
method="post" id="ajax_form" class="form-horizontal">
<div class="form-group colum-row row">
<div class="col-sm-6">
<input type="text" id="name" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="col-sm-6">
<input type="email" id="email" name="email" class="form-control" placeholder="Email" required>
</div>
</div>
<div class="form-group colum-row row">
<div class="col-sm-6">
<input type="tel" id="tel" name="tel" class="form-control" pattern="[0-9]{10}" placeholder="Phone 0452534200" required>
</div>
<div class="col-sm-6">
<input type="text" id="loc" name="loc" class="form-control" placeholder="Project Location">
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<textarea id="message" name="message" cols="30" rows="5" class="form-control message" placeholder="Message" required></textarea>
</div>
<div class="g-recaptcha col-sm-6 mt-2" data-sitekey="6LfRXOEhAAAAAJMek8MJn-kTKIzO-AKfg7JIp3zu"></div>
</div>
<div class="form-group row">
<div class="col-md-12">
<button id="submit" class="default-btn" type="submit">Send Message</button>
</div>
</div>
<div id="form-messages" class="alert" role="alert"></div>
</form>
</div>
</div>
</div>
</div>
</section>
<!-- begin snippet: js hide: false console: true babel: false -->
i am first time asking here, I don't know php coding ,i have one contact form on my webpage ,after filling which i get emails but twice in my inbox, I really don't know what changes should make in scripting here.i have added recptcha code here too by refering videos and docs but in php scripting there getting problem after submitting form
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['tel']) && isset($_POST['loc']) && isset($_POST['message'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$loc = $_POST['loc'];
$message = $_POST['message'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Kindly provide valid Email Address.";
} else {
$body = $name . "\n" . $email . "\n" . $tel . "\n" . $loc . "\n" . $message;
if (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email)) {
echo (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email))."Thanks for contacting us.";
} else {
echo "<script>alert('Sorry!, there is a problem in sending email. Please call us on +61 452 534 200')</script>";
}
}
} else {
echo 'Message could not be sent.';
}
$curlx = curl_init();
curl_setopt($curlx, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($curlx, CURLOPT_HEADER, 0);
curl_setopt($curlx, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlx, CURLOPT_POST, 1);
$post_data =
[
'secret' => '6LfRXOEhAAAAAOWyRGugvbPdYOhkztlz1aMcEwKP', //<--- your reCaptcha secret key
'response' => $_POST['g-recaptcha-response']
];
curl_setopt($curlx, CURLOPT_POSTFIELDS, $post_data);
$resp = json_decode(curl_exec($curlx));
curl_close($curlx);
if ($resp->success)
{
echo "<script>alert('Thanks For connecting us..we will get back to you soon..')</script>";
} else
{
// failed
echo "<script>alert('Please verify you are human..')</script>";
exit;
}
?>
<div class="col-md-6 padding-15">
<div class="contact-form">
<!-- https://brickboys.com.au/contact.php -->
<form action ="https://brickboys.com.au/contact.php"
method="post" id="ajax_form" class="form-horizontal">
<div class="form-group colum-row row">
<div class="col-sm-6">
<input type="text" id="name" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="col-sm-6">
<input type="email" id="email" name="email" class="form-control" placeholder="Email" required>
</div>
</div>
<div class="form-group colum-row row">
<div class="col-sm-6">
<input type="tel" id="tel" name="tel" class="form-control" pattern="[0-9]{10}" placeholder="Phone 0452534200" required>
</div>
<div class="col-sm-6">
<input type="text" id="loc" name="loc" class="form-control" placeholder="Project Location">
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<textarea id="message" name="message" cols="30" rows="5" class="form-control message" placeholder="Message" required></textarea>
</div>
<div class="g-recaptcha col-sm-6 mt-2" data-sitekey="6LfRXOEhAAAAAJMek8MJn-kTKIzO-AKfg7JIp3zu"></div>
</div>
<div class="form-group row">
<div class="col-md-12">
<button id="submit" class="default-btn" type="submit">Send Message</button>
</div>
</div>
<div id="form-messages" class="alert" role="alert"></div>
</form>
</div>
</div>
</div>
</div>
</section>
if (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email)) {
echo (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email))."Thanks for contacting us.";
}
The problem in these lines you're calling mail() function within the if statement. You just need to echo your message there doesn't need to call the mail function again.
This is how you'll write your if statement.
if (mail('services#brickboys.com.au', 'Service Enquiry', $body, 'From:' . $email)) {
echo "Thanks for contacting us.";
}
so I'm trying to make the contact form using PHP with validations. So when the input fields are not filled and I click send button it displays the error that says to fill in all blanks but when I fill in everything and click on send button, it sends message to my email address but doesn't display that the message was sent successfully, it just says "page not found" here's my code for contact.php and send.php
<div class="container">
<div class="row text-center">
<div class="col-12 heading">
<h1>Contact Us</h1>
</div>
<div class="col-12">
<?php
$Msg = "";
if(isset($_GET['error'])) {
$Msg = "Please Fill in All Blannks";
echo '<div class="alert alert-danger">'.$Msg.'</div>';
}
if(isset($_GET['success'])) {
$Msg = "Your Message Has Been Sent";
echo '<div class="alert alert-success">'.$Msg.'</div>';
}
?>
</div>
</div>
</div>
<div class="container">
<div class="row text-center">
<div class="col-12">
<form class="reportform" action="send.php" method="post">
<input type="text" name="UName" placeholder="Full Name"><br>
<input type="email" name="Email" placeholder="Your e-mail"><br>
<input type="text" name="Subject" placeholder="Subject"><br>
<textarea name="msg" placeholder="Message"></textarea><br>
<h6>By clicking send button, you agree our <a target="_blank" href="../credits/ppolicy.html">Privacy Policy</a></h6>
<button type="submit" name="btn-send" class="btn btn-primary sendbtn">Send</button>
</form>
</div>
</div>
</div>
<?php
if(isset($_POST['btn-send'])){
$UserName = $_POST['UName'];
$Email = $_POST['Email'];
$Subject = $_POST['Subject'];
$Msg = $_POST['msg'];
if(empty($UserName) || empty($Email) || empty($Subject) || empty($Msg)) {
header('location: contact.php?error');
}
else {
$to = "nika.makhatadze17#gmail.com";
if(mail($to,$Subject,$Msg,$Email)) {
header("location: index.php?success");
}
}
}
else {
header("location: contact.php");
}
?>
By Success you redirect to index.php?success but you defined the Success case in your contact.php, so i would say you just have to change header("location: index.php?success"); to header("location: contact.php?success");
Hey try this code we mead some changes on your code
pass success msg value on header location
<div class="container">
<div class="row text-center">
<div class="col-12 heading">
<h1>Contact Us</h1>
</div>
<div class="col-12">
<?php
$Msg = "";
if(isset($_GET['error'])) {
$Msg = "Please Fill in All Blannks";
echo '<div class="alert alert-danger">'.$Msg.'</div>';
}
if(isset($_GET['success'])) {
$Msg = "Your Message Has Been Sent";
echo '<div class="alert alert-success">'.$Msg.'</div>';
}
?>
</div>
</div>
</div>
<div class="container">
<div class="row text-center">
<div class="col-12">
<form class="reportform" action="send.php" method="post">
<input type="text" name="UName" placeholder="Full Name"><br>
<input type="email" name="Email" placeholder="Your e-mail"><br>
<input type="text" name="Subject" placeholder="Subject"><br>
<textarea name="msg" placeholder="Message"></textarea><br>
<h6>By clicking send button, you agree our <a target="_blank" href="../credits/ppolicy.html">Privacy Policy</a></h6>
<button type="submit" name="btn-send" class="btn btn-primary sendbtn">Send</button>
</form>
</div>
</div>
</div>
<?php
if(isset($_POST['btn-send'])){
$UserName = $_POST['UName'];
$Email = $_POST['Email'];
$Subject = $_POST['Subject'];
$Msg = $_POST['msg'];
$successMsg = 1;
if(empty($UserName) || empty($Email) || empty($Subject) || empty($Msg)) {
header('location: contact.php?error');
}
else {
$to = "nika.makhatadze17#gmail.com";
if(mail($to,$Subject,$Msg,$Email)) {
header("location: index.php?success=".$successMsg);
}
}
}
else {
header("location: contact.php");
}
im trying to send the email and redirect it, here is my code so far.
<?php
if (isset($_POST['formName'], $_POST['formEmail'], $_POST['formSubject'], $_POST['formMessage'])) {
$errors = array();
$formName = sanitize($_POST['formName']);
$formEmail = sanitize($_POST['formEmail']);
$formSubject = sanitize($_POST['formSubject']);
$formMessage = sanitize($_POST['formMessage']);
$required_fields = array('formName', 'formEmail', 'formSubject', 'formMessage');
foreach($_POST as $key=>$value){
if(empty($value) && in_array($key, $_required_fields) === true){
$errors[] = 'Fields marked with an asterisk are required';
break 1;
}
}
if(empty($errors)){
$to = 'pwnd999#msn.com';
$subject = $formSubject;
$body = $formMessage;
$header = 'From: ' . $formEmail;
wp_mail($to, $suject, $body, $header);
wp_redirect( get_permalink() . '?success' );
exit;
} else {
$errors[] = 'Something when wrong!';
}
function sanitize($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
if (isset($_GET[ 'success'])==true) { ?>
<div class="messageSent pannel">
<h1>Message sent successfully!</h1>
<p>I will try to reply as fast as possible however, feel free to send me a text message!</p>
</div>
<?php } else{ ?>
<form id="contactForm" method="POST" autocomplete="off" action="">
<?php if(!empty($errors)): ?>
<div class="errors pannel row">
<div class="col-md-8">
<ul>
<h3><li><?php echo implode('</li><li>', $errors);?></li><h3> </ul>
</div>
<div class="col-md-4">
<?php include(TEMPLATEPATH ."/panels/SVG/sadeFace.php"); ?>
</div>
</div>
<?php endif; ?>
<div class="form-group">
<label for="name"><h3>Name <span class="star">*</span></h3></label>
<span class="error"><?php echo $nameErr;?></span>
<div class="inputWrap">
<input type="text" required autocomplete="off" class="form-control" id="name" name="name" placeholder="Danny Devito" <?php echo isset($formName) ? 'value="' . $formName . '"' : '' ?>>
<?php include(TEMPLATEPATH ."/panels/SVG/name.php"); ?>
</div>
</div>
<div class="form-group">
<label for="email">
<h3>Email <span class="star">*</span></h3></label>
<div class="inputWrap">
<input type="email" required autocomplete="off" class="form-control" name="email" id="email" placeholder="devito#hollywood.com" <?php echo isset($formEmail) ? 'value="' . $formEmail . '"' : '' ?>>
<?php include(TEMPLATEPATH ."/panels/SVG/email.php"); ?>
</div>
</div>
<div class="form-group">
<label for="subject">
<h3>Subject <span class="star">*</span></h3></label>
<div class="inputWrap">
<input type="text" required autocomplete="off" class="form-control" name="subject" id="subject" placeholder="Design me a website!" <?php echo isset($formSubject) ? 'value="' . $formSubject . '"' : '' ?>>
<?php include(TEMPLATEPATH ."/panels/SVG/idea.php"); ?>
</div>
</div>
<div class="form-group">
<label for="message">
<h3>What can I help you with? <span class="star">*</span></h3></label>
<div class="inputWrap">
<textarea required autocomplete="off" name="message" class="form-control" id="message" placeholder="Hello Carl," value="poo">
<?php echo isset($formMessage) ? $formMessage : '' ?>
</textarea>
<?php include(TEMPLATEPATH ."/panels/SVG/message.php"); ?>
</div>
</div>
<button type="submit" name="submitted" class="btn main">Submit</button>
</form>
<?php }
?>
This is caused by the name of your field Name (name="name") that conflicts with the name parameter of WP_Query.
If you change that into name="formName" it should work.
Also you need to change the names of your other fields, not because of the previous reason, but because they don't match the names at the top of your code.
This question gets asked all the time, but I can't figure out why mine isn't working. I have a form that redirects to itself. If PHP decides it is submitted, there is a success/failure message and it displays the user input as the default value and disables the fields: using phpinfo I can see that the form is being submitted, but this first conditional doesn't work. I've tried a couple of versions, but no luck. It's weird because it sends the email
Specifically, the result and disable functions don't display their code after the form has been sent.
<?php
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}
function result(){
if($sent) echo $result;
}
function disable($field){
if($sent){
if($field != null){
$ret .= $field . '", disabled, placeholder!="';
}
$ret .= '", disabled, placeholder!="';
echo $ret;
}
}
function option($item){
$ret = "<option>";
if($sent){
if($eventType == $item){
$ret = "<option selected>";
}
}
$ret .= $item . "</option>";
echo $ret;
}
if(isset($_POST['name'])){
$sent = TRUE;
$result = null;
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$eventDate = $_POST['eventDate'];
$eventTime = $_POST['eventTime'];
$eventLength = $_POST['eventLength'];
$eventLocation = $_POST['eventLocation'];
$eventType = $_POST['eventType'];
$message = $_POST['message'];
$recipient = "";
$subject = " Form Submission";
$mailheader = "From: \r\n";
$formcontents = "You received this e-mail message through your website: \n\n";
$formcontents .= "Name: " . clean($name) . "\r\n";
$formcontents .= "Phone: " . clean($phone) . "\r\n";
$formcontents .= "Email: " . clean($email) . "\r\n";
$formcontents .= "Event Date: " . clean($eventDate) . "\r\n";
$formcontents .= "Event Time: " . clean($eventTime) . "\r\n";
$formcontents .= "Event Length: " . clean($eventLength) . "\r\n";
$formcontents .= "Event Location: " . clean($eventLocation) . "\r\n";
$formcontents .= "Event Type: " . clean($eventType) . "\r\n";
$formcontents .= "Message: " . clean($message) . "\r\n";
$formcontents .= "\r\n";
$formcontents .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
$formcontents .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n";
// Send mail
if(mail($recipient, $subject, $formcontents, $mailheader)){;
$result = '<h3 class="alert alert-success"> Thank you, your form was successfully sent and I will contact you shortly.</h3>';
} else {
$result = '<h3 class="alert alert-error"> Your mail could not be sent at this time.</h3>';
}
}
?>
<form action="contact.php" method="POST" class="form-horizontal span4">
<fieldset>
<legend>
<h2>Or send me a message. </h2>
</legend>
<p class="help-block">None of the fields are required, but the more information I have about your event, the more detailed I can be in my response.</p>
<legend class="help-block">Your Details</legend>
<div class="control-group">
<label for="name" class="control-label">Your Name</label>
<div class="controls">
<input id="name" type="text" name="name" placeholder="<?php disable($name); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="phone" class="control-label">Your Contact Number</label>
<div class="controls">
<input id="phone" type="tel" name="phone" placeholder="<?php disable($phone); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="email" class="control-label">Your Email</label>
<div class="controls">
<input id="email" type="email" name="email" placeholder="<?php disable($email); ?>" class="input-xlarge"/>
</div>
</div>
<legend class="help-block">Your Event </legend>
<div class="control-group">
<label for="eventDate" class="control-label">Your Event's Date</label>
<div class="controls">
<input id="eventDate" type="date" name="eventDate" placeholder="<?php disable($eventDate); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventTime" class="control-label">Your Event's Start Time</label>
<div class="controls">
<input id="eventTime" type="time" name="eventTime" placeholder="<?php disable($eventTime); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventLength" class="control-label">Your Event's Length</label>
<div class="controls">
<input id="eventLength" type="text" name="eventLength" placeholder="<?php disable($eventLength); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventLocation" class="control-label">Your Event's Location</label>
<div class="controls">
<input id="eventLocation" type="text" name="eventLocation" placeholder="<?php disable($eventLocation); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventType" class="control-label">What Kind of Event</label>
<div class="controls">
<select id="eventType" name="eventType" placeholder="<?php disable($eventType); ?>"><?php option("Charity Event"); option("Expo/Trade Show"); option("Personal Event"); option("Other"); ?></select>
</div>
</div>
<div class="control-group">
<label for="message" class="control-label">Other comments or the best time to reach you.</label>
<div class="controls">
<textarea id="message" name="message" rows="10" placeholder="<?php disable($message); ?>" class="input-xxlarge"></textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" name="submit" placeholder="<?php disable(null); ?>" class="btn btn-primary">Send Message</button>
</div>
</fieldset>
</form>
You have to import your global variables into function scope, like:
function result(){
global $sent, $result;
if($sent) echo $result;
}
..in functions disable() and option(), too.
if(isset($_POST['name'])){
should be
if(isset($_POST['submit'])){