PHP contact form will not submit - php

I have a simple php contact form i got from a web tutorial. It worked yesterday, but will not work today. I'd love some help, as I don;t know much php.
php:
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!filter_var( trim($_POST['email'], FILTER_VALIDATE_EMAIL ))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'person#domain.com'; // Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
HTML:
<form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="alert alert-danger">Please check if you've filled all the fields with valid information and try again. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<div class="alert alert-success">
<p><strong>Message Successfully Sent!</strong></p>
<p>Thank you for using our contact form, <strong><?php echo $name;?></strong>! Your email was successfully sent and we’ll be in touch with you soon.</p>
</div>
<?php } ?>
<div class="form-group">
<label for="name">Your Name<span class="help-required">*</span></label>
<input type="text" name="contactname" id="contactname" value="" class="form-control required" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="email">Your Email<span class="help-required">*</span></label>
<input type="text" name="email" id="email" value="" class="form-control required email" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="email" id="subject" class="form-control required" role="input" aria-required="true">
</div>
<div class="form-group">
<label for="message">Message<span class="help-required">*</span></label>
<textarea rows="8" name="message" id="message" class="form-control required" role="textbox" aria-required="true"></textarea>
</div>
<div class="actions">
<input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-grey" title="Click here to submit your message!" />
<input type="reset" value="Clear Form" class="btn btn-grey pull-right" title="Remove all the data from the form." />
</div>
</form>
It gets hung up on the validation. Not sure why.

$_POST["subject] is not defined in your form. Your SUBJECT field is called EMAIL:
Change:
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="email" id="subject" class="form-control required" role="input" aria-required="true">
</div>
With:
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="subject" id="subject" class="form-control required" role="input" aria-required="true">
</div>

Related

How to make recaptcha work with verification

I tried this in my code and I entered the right secret key just didn't want to put it out there but everytime I submit my form even with it checked it shows up with the error that it isn't filled out, without the captacha stuff in the form it works just fine. Can someone please help me fix this issue I would like it to make sure you filled it out before it sends it to my email!!
<h2 class="text-center" id="whatwedo">Contact form</h2><hr class="titlehr"></div></div><br>
<?php
if (isset($_POST["submit"])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$description = $_POST['description'];
$captcha = $_POST['g-recaptcha-response'];
$from = $fullname;
$to = 'mcgarrywebdesign#gmail.com';
$subject = 'Contact Form';
$body = "From: $fullname\n E-Mail: $email\n Phone: $phone\n Subject: $subject\n description: $description";
// Check if name has been entered
if (!$_POST['fullname']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['phone']) {
$errPhone = 'Please enter your phone number';
}
if (!$_POST['subject']) {
$errSubject = 'Please enter the subject';
}
if (!$_POST['description']) {
$errDescription = 'Please enter the description';
}
if(!$captcha){
$errcaptcha = "Please check the the captcha form";
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errPhone && !$errSubject && !$errDescription && !$errcaptcha) {
$secretKey = "secret key";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
$result = "You are spammer ! Get the #$%K out";
} else {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! We will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}}
?>
<?php echo "<p class='text-danger'>$result</p>";?>
<div class="row">
<div class="col-md-12 col-lg-6"> <form action="contact.php" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Full Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Full Name" name="fullname">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Email Address</label>
<input type="email" class="form-control" id="exampleInputPassword1" placeholder="Enter Email" name="email">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Phone Number</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Phone Number" name="phone">
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="col-md-12 col-lg-6">
<div class="form-group">
<label for="exampleInputEmail1">Subject</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Subject" name="subject">
<?php echo "<p class='text-danger'>$errSubject</p>";?>
</div>
<div class="form-group">
<label for="exampleTextarea">Description</label>
<textarea class="form-control" id="exampleTextarea" rows="3" name="description" placeholder="Description"></textarea>
<?php echo "<p class='text-danger'>$errDescription</p>";?>
</div>
<br>
</div></div>
<div class="g-recaptcha text-center mx-auto d-block" data-sitekey="6LdhyXcUAAAAANrj8qTSLKcrbjVX6ij07Dqw0awe"></div>
<?php echo "<p class='text-danger'>$errcaptcha</p>";?>
<div class="row"><div class="col-lg-12"><button type="submit" name="submit" value="send" class="btn btn-primary mx-auto d-block" style="width: 190px !important;height: 60px !important;font-size: 25px;">Submit</button></div></div></form>
</div>

Simple HTML/PHP form takes forever to send email

I have this simple email form using the POST function with a PHP mailto() function. I have PHP lines within my form as values to the fields. It takes way too long to send. Any help is appreciated.
PHP:
<?php
if ($_POST['submit']) {
if (!$_POST['name']) {
$error="<br/>-Please enter your name";
}
if (!$_POST['email']) {
$error.="<br/>-Please enter your email";
}
if (!$_POST['message']) {
$error.="<br/>-Please enter a message";
}
if (!$_POST['check']) {
$error.="<br/>-Please confirm you're real";
}
if ($error){
$result="Ohh no, somethings not right... $error";
}
else {
mail("email#email.com","website contact form","Name: ".$_POST['name']."
Email: ".$_POST['email']."
Message: ".$_POST['message']);
{
$result="Thank you, I will reply shortly.";
}
}
}
?>
HTML:
<form action="#contactme" method="post" role="form">
<div class="form-group">
<input type="text" name="name" placeholder="Enter your name" class="form-control form-control-lg" value="<? echo $_POST['name']; ?>" >
</div>
<div class="form-group">
<input type="email" name="email" placeholder="Enter your email" class="form-control form-control-lg" value="<? echo $_POST['email']; ?>">
</div>
<div class="form-group">
<textarea name="message" rows="5" class="form-control" placeholder="Your message..."><? echo $_POST['message']; ?> </textarea>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="check"><span class="real"> I am real</span>
</label>
</div>
<div align="center">
<input type="submit" name="submit" class="btn btn-secondary btn-lg" value="Send">
</div>
</form>
I have done everything in a query now it should be sent faster but it can also be that this code has an error but the first query is correct from the structure.
<?php
if ($_POST['submit']) {
if (!$_POST['name'] || !$_POST['email'] || !$_POST['message'] || !$_POST['check'] ||) {
$error="<br/>-Please enter";
}else
{
if ($error){
$result="Ohh no, somethings not right... $error";
}
else {
mail("email#email.com","website contact form","Name: ".$_POST['name']."
Email: ".$_POST['email']."
Message: ".$_POST['message']);
{
$result="Thank you, I will reply shortly.";
}
} <-- If you have a mistake, take a clip away.
}
}
?>

How do I stop the Phantom Emails from my PHP code?

I'm currently using an html5 website with a contact form and a php action to receive contact emails. I've tried a few different codes in my php but non have stopped the blank emails from coming. I do have google analytics enables on my code but have blocked the crawler through robot.txt. Here is my code.
PHP CODE
<?php
foreach ($_GET as $Field=>$Value) {
if($Value != ''){
$body .= "$Field: $Value\n";
}
}
$name = trim(htmlentities($_GET['name'],ENT_QUOTES,'utf-8'));
$email = trim(htmlentities($_GET['email'],ENT_QUOTES,'utf-8'));
$phone = trim(htmlentities($_GET['phone'],ENT_QUOTES,'utf-8'));
$messages = trim(htmlentities($_REQUEST['messages'],ENT_QUOTES,'utf-8'));
if (strlen($name) == 0 )
{
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
if (strlen($email) == 0 )
{
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
$to = "junior.8791#gmail.com";
$message = "Name: ".$name;
$message.="\n\nEmail: ".$email;
$message.="\n\nPhone: ".$phone;
$message .= "\n\nMessage: ".$messages;
$headers = "From: $email";
$headers .="\nReply-To: $email";
$success = mail($to, $subject, $message, $headers);
if ($success) {
echo "<script>window.location = 'http://www.mason372.org/thankyou.html'</script>";
} else {
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
?>
CONTACT FORM
<form action="email.php" method="post" id="form">
<div class="5grid">
<div class="row">
<div class="6u">
<input type="text" class="name" name="name" id="name" placeholder="Name" value="" aria-describedby="name-format" required aria-required=”true” pattern="[A-Za-z-0-9]+\s[A-Za-z-'0-9]+" title="e.g.'John Doe'" required="" />
</div>
<div class="6u">
<input type="email" class="email" name="email" id="email" placeholder="Email" required="" />
</div>
</div>
<div class="row">
<div class="12u">
<input type="tel" class="tel" name="phone" id="phone" name="phone" type="text" placeholder="Phone Number" pattern="(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}" required />
</div>
</div>
<div class="row">
<div class="12u">
<textarea name="messages" id="messages" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input type="submit" class="button" value="Send Message">
<input type="reset" class="button button-alt" value="Clear Form">

How to make a send yourself a copy check box

I need a check box that can send yourself a copy. For example, they've filled out the contact form and want a copy sent to their self, they check the box and it will email it to me and still email it to them. Here's my PHP:
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['weburl']) == '') {
$site = trim($_POST['weburl']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'myemail#domain.com'; // Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nSite: \n\nComments:\n $comments";
$headers = 'From: BTSyncrets Contact <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
Here's my contact form code:
<div id="contact" class="offset4 login">
<form style="margin-top: 5% !important;" method="post" action="index.php" id="contactform">
<fieldset class="well">
<br>
<div class="clearfix">
<label for="name">
Your Name<span class="help-required">*</span>
</label>
<div class="input">
<input type="text" id="boxblack" name="contactname" id="contactname" value="" class="span6 required" role="input" aria-required="true" />
</div>
</div>
<div class="clearfix">
<label for="email">
Your Email<span class="help-required">*</span>
</label>
<div class="input">
<input type="text" id="boxblack" name="email" id="email" value="" class="span6 required email" role="input" aria-required="true" />
</div>
</div>
<div class="clearfix">
<label for="weburl">
Your Website
</label>
<div class="input">
<input type="text" id="boxblack" name="weburl" id="weburl" value="" class="span6 required url" role="input" aria-required="true" />
</div>
</div>
<div class="clearfix">
<label for="subject">
Subject<span class="help-required">*</span>
</label>
<div class="input">
<select name="subject" id="boxblack" id="subject" class="span6 required" role="select" aria-required="true">
<option></option>
<option>One</option>
<option>Two</option>
</select>
</div>
</div>
<div class="clearfix">
<label for="message">Message<span class="help-required">*</span></label>
<div class="input">
<textarea rows="8" id="boxblack" style="resize: none;" name="message" id="message" class="span6 required" role="textbox" aria-required="true"></textarea>
</div>
</div>
<label class="checkbox">
<input type="checkbox" name="copy" value="1" /> Send Yourself a copy
</label>
<div class="actions">
<input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-inverse" title="Click here to submit your message!" />
<input type="reset" value="Clear Form" class="btn btn-danger" title="Remove all the data from the form." />
</div>
</fieldset>
</form>
</div><!-- form -->
See the Add this comment below.
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'myemail#domain.com'; // Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nSite: \n\nComments:\n $comments";
$headers = 'From: BTSyncrets Contact <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
// Add this
if (isset($_POST['copy'])) {
$headers .= "\nBcc: myemailaddress#example.com";
}
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}

Contact Form: Why won't this form validate?

I have a contact form at the bottom of a, (single page business/portfolio), website.
The script above my DOCTYPE looks like this.
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['comment']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'myemail#gmail.com'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
The JQuery validation does work.
<script type="text/javascript">
$(document).ready(function(){
$("form").validate();
});
</script>
The contact form:
<div class="grid_8">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Email Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>
<?php } ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<label for="name">Your Name:</label>
<div>
<input type="text" name="contactname" class="required" />
</div>
</div>
<div>
<label for="email">Your Email:</label>
<div>
<input type="text" name="email" class="required email" />
</div>
</div>
<div>
<label for="subject">Subject:</label>
<div>
<input type="text" name="subject" class="required" />
</div>
</div>
<div>
<label for="comments">Comments:</label>
<div>
<textarea name="comment" name="comment" class="required"></textarea>
</div>
</div>
<div>
<input id="button" type="submit" value="SEND" />
</div>
</form>
</div>
When you submit the form, the email does not get sent, nor any verification from php. What am I doing wrong?
One quick obvious issue, although I have many less critical ones. Simply put you'r checking for $_POST['submit'] put there is name="submit" in your form.
So change:
<input id="button" type="submit" value="SEND" />
To:
<input id="button" type="submit" name="submit" value="SEND" />
Or change:
if(isset($_POST['submit']))
To:
if(count($_POST))
// OR
if(!empty($_POST))
Either one will fix your problem
I don't see why isset($_POST['submit']) should return true when you have no field with name="submit" in your form.
What I prefer to do is using an array for all the required data:
<!-- ... -->
<input type="text" name="mail[contactname]" class="required" />
<!-- ... -->
<input type="text" name="mail[email]" class="required email" />
<!-- ... -->
<input type="text" name="mail[subject]" class="required" />
<!-- ... -->
<textarea name="comment" name="mail[comment]" class="required"></textarea>
<!-- ... -->
And then asking for that array
if (isset($_POST['mail']))
// ...

Categories