Recaptcha in Form - php

I have almost the same form as given the following URL:
URL: PHP form + Google reCAPTCHA
But after including the PHP code to my website-(www.obzservices.com) given in the answer, my website is just showing me the pre-loader icon i.e. my website is not loading up. I have edit my PHP Code previously by adding my Site Key, adding $name, $title, $location etc but it didn't help. Here is my PHP code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$title = $_POST['title'];
$location = $_POST['location'];
$industry = $_POST['industry'];
$quantity = $_POST['quantity'];
$message = $_POST['message'];
$from = 'From: QuoteForm';
$to = 'obaid#obzservices.com';
$subject = 'Quote Request';
$body = "From: $name\n E-Mail: $email\n Title: $title\n Location: $location\n Industry: $industry\n Quantity: $quantity\n Message:\n $message";
if ($_POST['submit']) {
if ($email != '') {
if(mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
}?>

Add following simpleScript
<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
function get_action(form)
{
var v = grecaptcha.getResponse();
if(v.length == 0)
{
document.getElementById('captcha').innerHTML="You can't leave Captcha Code empty";
return false;
}
else
{
document.getElementById('captcha').innerHTML="Captcha completed";
return true;
}
}
</script>
and add this HTML before your submit button:
<span id="captcha" style="color:red" /></span>
<div class="g-recaptcha" id="rcaptcha" data-sitekey="site key"></div>
Add this to your form
onSubmit="return get_action()" such As below
<form action="" method="post" onSubmit="return get_action()">

Related

php form works on one site but not another

I've been using the same contact form for quite a while:
<?php
if (isset($_POST['submit']) ) {
$name = $_POST['names'];
$email = $_POST['email'];
$message = $_POST['message'];
$robot = $_POST['robot'];
$from = 'online#domain.co.uk';
$to = 'info#domain.co.uk';
$subject = 'Online Enquiry';
$headers = "From: DOMAIN <no-reply#domain.co.uk> \r\n";
$headers .= 'Reply-To:'. $email . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion(); // Sender's Email
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
} ?>
<?php if (isset($_POST['submit'])) {
if ($name != '' && $email != '' && $message !='') {
if ($robot != 'yes') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p class="approved"><strong>Your message has been submitted</strong></p>';
} else {
echo '<p class="warning"><strong>Something went wrong!</strong></p>';
}
} else if ($_POST['submit'] && $robot == 'yes') {
echo '<p class="warning"><strong>Looks like you are spam!</strong></p>';
}
} else {
echo '<p class="warning"><strong>Please complete all fields.</strong></p>';
}
}
?>
<form id="contact" method="post" action="#contact">
<label>Name</label>
<input name="names" type="text" placeholder="NAME">
<label>Email</label>
<input name="email" type="email" placeholder="EMAIL">
<label>Message</label>
<textarea rows="10" name="message" placeholder="MESSAGE"></textarea>
<input id="capture" name="robot" type="checkbox" value="yes">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
It works absolutely fine on other sites but doesn't work on one domain? submit simply returns the error "Something went wrong!". the site is on the same cloud server as many of the other sites that work fine. It's driving me mad because I cant see an error.
Turning debugging on I get the error:
Notice: Undefined index: robot in /form-contact.php on line 6
Ive tried removing the robot check all together and it still doesn't work, but generates no error when debugging?
Can anyone offer any advice?
When your checkbox is not checked - it will not be sent.
Replace
$robot = $_POST['robot'];
to
$robot = isset($_POST['robot']) ? 'yes' : 'no';
Your Condition Matches the case. Try checking the value of $robot on submit.
<input id="capture" name="robot" type="checkbox" value="yes">
if ($robot != 'yes') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p class="approved"><strong>Your message has been submitted</strong></p>';
} else {
echo '<p class="warning"><strong>Something went wrong!</strong></p>';
}

Changing the value of a Form Submit Button to say submitted after form submits

I have a contact form on my website. I'd like to change the text of the submit button to say "submitted" after the form has successfully submitted, and maybe even make it say "submitting" while the form is submitting. I am unsure of how to do this, i could do an onclick event that would change the text, but not the route i want to take as the message could fail to send and the button would still say submitted.
Here is my html for the form
<form method="post" action="contact.php">
<input type="text" name="name" placeholder="Name"><br>
<input type="email" name="email" placeholder="Email"><br>
<textarea rows="8" cols="65" name="message"placeholder="Message"></textarea><br>
<input id="submit" type="submit" name="submit" value="Let's Get In Touch">
</form>
and here is my php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Portfolio Website';
$to = 'kyle.a.binger#gmail.com';
$subject = 'Message From Personal Site';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
Is there a way to do this with my existing php code? Thanks in advance for any help.
What you are going to need is something to pass the data to the php script, and return something/echo something back without leaving the page.
Take a look into AJAX. You will be able to exactly this.
Here's a link to one of the first posts on stackoverflow that showed up.
Here's a link to w3schools to give you a quick example/idea.
If you don't want to use AJAX and you're posting to page itself you can do the following
<form method="post" action=""> <!-- removed the PHP file name to post to itself -->
<input type="text" name="name" placeholder="Name"><br>
<input type="email" name="email" placeholder="Email"><br>
<textarea rows="8" cols="65" name="message"placeholder="Message"> </textarea><br>
<?php
if (isset($_POST['submit'])) {
echo '<input id="submit" type="button" name="submit" value="Submitted">'; //Changed type submit to button
} else {
echo '<input id="submit" type="submit" name="submit" value="Let\'s Get In Touch">';
}
?>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Portfolio Website';
$to = 'kyle.a.binger#gmail.com';
$subject = 'Message From Personal Site';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>

Hide Email form after sumbission

I have a PHP email form embedded at the top of the HTML of my contact form page (index.php):
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'email#example.com';
$subject = 'Message Contact Form ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
} else {
$result='<div>Sorry there was an error sending your message.</div>';
}
}
}
?>
HTML:
<form role="form" method="post" action="index.php">
...
</form>
and I wanted to hide the form only after successful submission. How can I do this?
In your CSS file;
.hide {
display:none;
}
(If unsure how to do this, this question should help)
Then inside your PHP if block;
$class = "";
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
$class = 'class="hide"';
} else {
$result='<div>Sorry there was an error sending your message.</div>';
}
and finally, on index.php:
<form role="form" method="post" action="index.php" <?php echo $class ?>>
Now you can hide the entire form, with a little bit of CSS and a dynamic variable.
Edit:
If you wanted to avoid using an external or internal CSS file entirely, you could apply the CSS inline to your html element directly, like so;
$style = 'style = "display:none;"';
An alternative approach, if you're not using style sheets or any other CSS.
To hide form after submission and email success,give id to form(here its id="myForm", then use style to display:none as shown below)
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'email#example.com';
$subject = 'Message Contact Form ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
echo "<style> #myForm{ display:none; } </style>";
} else {
$result='<div>Sorry there was an error sending your message.</div>';
echo "<style> #myForm{ display:block; } </style>";
}
}
}
?>
<form role="form" method="post" action="index.php" id="myForm">
</form>

php mail() script not working

Hey guys I cannot for the life of me figure out where I went wrong here. When the form submits it takes it to a blank page. No errors or confirmed. Just Blank. Im assuming it is a syntax error but I just cant see it for some reason. Do you guys see anything?
Here is my form code:
<form method="POST" action="mailtest.php">
<label>Name<span class="req">*</span></label>
<input name="name" placeholder="Type Here">
<label>Email<span class="req">*</span></label>
<input name="email" type="email" placeholder="Type Here">
<label>Subject</label>
<input name="subjectf" placeholder="Type Here">
<label>Message<span class="req">*</span></label>
<textarea name="message" placeholder="Type Here"></textarea>
<input class="submit" name="submit" type="submit" value="">
<span class="right" style="color:red">* represents a mandatory field.</span>
</form>
And here is the php script I have on the mailtest.php page:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['name'];
$to = 'rkoolman#bellsouth.net';
$subject= 'new enquiry on website';
$subjectf= $_POST['subjectf'];
$body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '' && $message != '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
}
?>
You should always first check whether the form was submitted so
remove if ($_POST['submit']) and put it on the top of the script..
also you should check whther the $_POST["submit"] was set.. the script should look like this:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['name'];
$to = 'test#test.com';
$subject= 'new enquiry on website';
$subjectf= $_POST['subjectf'];
$body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";
if ($name != '' && $email != '' && $message != '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
}
Set your submit input value to submit
<input class="submit" type="submit" name="submit" value="submit">

PHP thank you only on successful submission

So I have a form with both javascript validation to check each field is filed and a jquery ajax script to stop the page reloading.
The PHP script is very simple and only emails the fields of the form.
The problem is I need to ONLY show the thank you message if each field is filled out, and at the moment it appears to be showing it when I just enter the name field.
It doesn't show it if i just enter either the email or message field though and I can't work out why (I'm bad at PHP).
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$priority = $_POST['priority'];
$message = $_POST['message'];
$telephone = $_POST['telephone'];
$formcontent="From: $name \n Email: $email \n Telephone Number: $telephone \n Priority: $priority \n Message: $message";
$recipient = "myemail#address.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
if(mail($name,$email,$message,$headers)){
echo "<p>Thanks for your mail...</p>";
}
?>
Javascript that stops page reloading.
<script type="text/javascript">
$(document).ready(function() {
$(".contactus").submit(function() {
$.post("mail.php", $(".contactus").serialize(),
function(data) {
$("#formResponse").html(data);
}
);
return false;
});
});
</script>
Form validation.
<script type="text/javascript" language="JavaScript">
var FormName = "contactus";
var RequiredFields = "name,email,priority,message";
function ValidateRequiredFields()
{
var FieldList = RequiredFields.split(",")
var BadList = new Array();
for(var i = 0; i < FieldList.length; i++) {
var s = eval('document.' + FormName + '.' + FieldList[i] + '.value');
s = StripSpacesFromEnds(s);
if(s.length < 1) { BadList.push(FieldList[i]); }
}
if(BadList.length < 1) { return true; }
var ess = new String();
if(BadList.length > 1) { ess = 's'; }
var message = new String('\n\nThe following field' + ess + ' are required:\n');
for(var i = 0; i < BadList.length; i++) { message += '\n' + BadList[i]; }
alert(message);
return false;
}
function StripSpacesFromEnds(s)
{
while((s.indexOf(' ',0) == 0) && (s.length> 1)) {
s = s.substring(1,s.length);
}
while((s.lastIndexOf(' ') == (s.length - 1)) && (s.length> 1)) {
s = s.substring(0,(s.length - 1));
}
if((s.indexOf(' ',0) == 0) && (s.length == 1)) { s = ''; }
return s;
}
// -->
</script>
And the form
<form action="mail.php" id="myform" class="contactus" onsubmit="return ValidateRequiredFields(); this.submit(); this.reset(); return false;" name="contactus" method="POST">
<p class="floatleft" style="width:200px; background-color:#FF0000; height:50px; line-height:50px; margin:0; padding:0;">Nameeeeee</p> <input class="sizetext" type="text" maxlength="10" name="name">
<div style="clear:both"></div>
<p class="floatleft" style="width:200px;">Email</p> <input class="sizetext" type="text" maxlength="10" name="email">
<div style="clear:both"></div>
<p class="floatleft" style="width:200px;">Telephone</p> <input class="sizetext" type="text" maxlength="10" name="telephone">
<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
<option value="Emergency">Emergency</option>
</select>
</select>
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<br />
<input class="buttonstyle" type="submit" value="Send">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$priority = $_POST['priority'];
$message = $_POST['message'];
$telephone = $_POST['telephone'];
$check = array('name', 'email', 'priority', 'message', 'telephone');
$send = true;
foreach($check as $key => $value){
if(empty($_POST[$value])){
$send = false;
}
}
$formcontent="From: $name \n Email: $email \n Telephone Number: $telephone \n Priority: $priority \n Message: $message";
$recipient = "myemail#address.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if($send && mail($recipient, $subject, $formcontent, $mailheader)){
echo "<p>Thanks for your mail...</p>";
}
else{
die('Error!');
}
Well, the most easier way would be to check if your POST fields are present and not empty. Probably there is a more elegant solution if you want to check every field individually, but here goes:
if (isset($_POST['name']) && !empty($_POST['name']) && isset($_POST['email']) && !empty($_POST['email']) && isset($_POST['message']) && !empty($_POST['message'])) {
if(mail($name,$email,$message,$headers)){
echo "<p>Thanks for your mail...</p>";
}
}
You need to wrap your code around
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$priority = $_POST['priority'];
$message = $_POST['message'];
$telephone = $_POST['telephone'];
$formcontent="From: $name \n Email: $email \n Telephone Number: $telephone \n Priority: $priority \n Message: $message";
$recipient = "myemail#address.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
$mail = #mail($recipient, $subject, $formcontent, $mailheader);
if($mail){
echo "<p>Thanks for your mail...</p>";
}
else
{
echo "Error Sending Mail " ;
}
}

Categories