I am trying to create a two page form- the first page is optional. Its a prompt to get started. It was working perfectly until I started adding in PHPMailer. Now when I click submit on the first, optional form on the first page it submits it through the mailer and all its validations/errors then emails correctly it if I comment out the error if statements.
Here is the first form that is optional. It should take you to the estimate page with those values stored in the form already so you don't have to do it over again.
<form class="form-horizontal first-form" action="estimate.php" method="post">
<div class="form-group">
<label for="inputMoveDate" class="col-xs-4 text-left">Move Date</label>
<div class="col-xs-8">
<input type="text" name="date" class="form-control" id="datepicker">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="pickUpZip" class="col-xs-4">Pick Up Zip</label>
<div class="col-xs-8">
<input type="text" name="pickUpZip" class="form-control" id="pickUpZip" placeholder="">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="dropOffZip" class="col-xs-4">Drop Off Zip</label>
<div class="col-xs-8">
<input type="text" name="dropOffZip" class="form-control" id="dropOffZip" placeholder="">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<div class="col-xs-12">
<button type="submit" class="btn btn-default center-block">Continue</button>
</div><!--col-sm-offset-2-->
</div><!--form-group-->
</form>
Estimate.php second page. The PHP at the top of the document.
session_start();
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$date = $_POST['date'];
$pickUpZip = $_POST['pickUpZip'];
$dropOffZip = $_POST['dropOffZip'];
$dwellingType = $_POST['dwellingType'];
$salutation = $_POST['salutation'];
$firstName = trim(filter_input(INPUT_POST, "first-name", FILTER_SANITIZE_STRING));
$lastName = trim(filter_input(INPUT_POST, "last-name", FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_SPECIAL_CHARS));
$phone = $_POST['phone'];
$pickUpAddress = $_POST['pickUpAddress'];
$pickUpCity = $_POST['pickUpCity'];
$pickUpState = $_POST['pickUpState'];
$comments = $_POST['comments'];
$emailBody ="From: $salutation $firstName $lastName\n
Phone Number: $phone\n
E-Mail: $email\n
Message:\n $comments
\n \n
Move Date: $date\n
Pick Up Address: $pickUpAddress\n
Pick Up City: $pickUpCity\n
Pick Up State: $pickUpState\n
Pick Up Zip: $pickUpZip\n
\n
Drop Off Zip: $dropOffZip\n
Dwelling Type: $dwellingType\n";
if($firstName == "" || $email == "" || $lastName == "") {
echo "Please fill in the required forms: first name, last name, and email.";
exit;
}
if ($_POST["address"] != "") {
echo "Bad form input";
exit;
}
require('phpmailer/class.phpmailer.php');
$mail = new PHPMailer;
if (!$mail->ValidateAddress($email)) {
echo 'Invalid email. Try again.';
exit;
}
$mail->setFrom($email, $firstName);
$mail->addAddress('xxxx#xxx.com', 'name here');
$mail->isHTML(false);
$mail->Subject = 'Estimate Request Inquiry From ' . $firstName;
$mail->Body = $emailBody;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
header("location:estimate.php?status=thanks");
}
Here is the starting and ending of the form HTML, the highlights.
<?PHP
if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
echo "Thank you for your estimate request. We will return your inquiry soon!
</div>";
} else {
?>
<form class="form-horizontal first-form" action="estimate.php" method="post" style="margin: 0 45px;">
<label for="date" class="col-xs-4 text-left">Move Date</label>
<div class="col-xs-8">
<input type="text" class="form-control" id="datepicker" name="date" value="<?php echo $_POST["date"]; ?>">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="pickUpZip" class="col-xs-4">Pick Up Zip</label>
<div class="col-xs-8">
<input type="text" name="pickUpZip" class="form-control" id="pickUpZip" value="<?php echo $_POST["pickUpZip"]; ?>">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="dropOffZip" class="col-xs-4">Drop Off Zip</label>
<div class="col-xs-8">
<input type="text" name="dropOffZip" class="form-control" id="dropOffZip" value="<?php echo $_POST["dropOffZip"]; ?>">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="col-xs-12">
<button type="submit" id="submit" name="submit" class="btn btn-default center-block">Submit</button>
</div><!--col-sm-offset-2-->
</div><!--form-group-->
</form>
<?PHP
}
?>
This line attempts to send the email every time:
if(!$mail->send()) {
and your error messages are echoed if and when it fails.
You should nest this entire block in an if statement that checks if the form was submitted from page 2. You can give the submit button a different value and check that (i.e. 'submit1', 'submit2').
Related
Hi I have the same issue. Just want to print out the value that was selected on the calendar and include it to send to my email. It prints out to the page but it does not include to the email.
PHP code:
<?php
if (isset($_POST['send'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = "Attendance Form";
$date = $_POST['date[]'];
$message = "Bro./Sis. ".$name." "."attendance will be at"." ".print_r(date($date['d,m,Y']));
$emailTo = "me#domain.com";
$mailHeaders = "From: " . $name . "<". $email .">\r\n";
if (mail($emailTo, $subject, $date, $mailHeaders)) {
$message ='<div class="alert alert-success d-inline-block" role="alert">Your message has been sent.</div>';
$type = "success";
} else {
$error = '<div class="alert alert-danger d-inline-block" role="alert">Something went wrong, please try again.</div>';
}
}
?>
Hi guys thank you for your comments. I used the datepicker but whenever I use the jqueryui i cannot pick the value so i put the date as a type. This is my HTML:
<div class="container-fluid text-center" id="attendform" style="top: 50%;transform: translateY(25%) !important;position: relative;">
<!--Form for join us-->
<div class="container bg-light p-5 mt-5 d-inline-block text-center">
<h2 class="join-title text-center display-4 mb-0">Join us!</h2><br><br>
<div class="mt-0 mb-3" id="error"><? echo $error.$message; ?></div>
<div class="container text-center">
<form class="" method="post">
<label for="name" class="text-dark border-top-0 border-right-0 border-left-0" >Name: </label> <br>
<input class="form-control" type="text" name="name" id="name" placeholder="Bro./Sis."><br>
<label for="email">Email</label>
<input type="email" name="email" class="form-control" id="email" placeholder="user#domain.com" required> <br>
<!---->
<label for="date" class="d-inline-block">Date: </label>
<input type="date" id="datepicker" name="date[]" class="form-control" value=""></input><br>
<input class="btn btn-dark" id="sendbtn" type="submit" name="send" value="Send">
</form>
</div>
</div>
</div>
</div>
I disable the jquery datepicker first and try with the normal calendar
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<!--script-->
<script src="js/jquery-3.4.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js" integrity="sha256-0YPKAwZP7Mp3ALMRVB2i8GXeEndvCq3eSl/WsAl1Ryk=" crossorigin="anonymous"></script>
<!-- <script type="text/javascript">
$(function() {
$("#datepicker").datepicker();
});
</script> -->
I finally figure the answer:
PHP code
<?php
if (isset($_POST['send'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = "Attendance Form";
$date = $_POST['date'];
$member = $_POST['member'];
$message = "Bro./Sis. ".$name." "."($member)"."\n\n"."Schedule:"." "."($date)";
$emailTo = "me#domain.com";
$mailHeaders = "From: " . $name . "<". $email .">\r\n";
if (mail($emailTo, $subject, $message, $mailHeaders)) {
$message ='<div class="alert alert-success d-inline-block" role="alert">Your message has been sent.</div>';
$type = "success";
} else {
$error = '<div class="alert alert-danger d-inline-block" role="alert">Something went wrong, please try again.</div>';
}
}
?>
I edited my form and put the jquery datepicker
<form class="" method="post">
<label for="name" class="text-dark border-top-0 border-right-0 border-left-0" >Name: </label> <br>
<input class="form-control" type="text" name="name" id="name" placeholder="Bro./Sis."><br>
<label for="email">Email</label>
<input type="email" name="email" class="form-control" id="email" placeholder="user#domain.com" required> <br>
<!---->
<label for="date" class="d-inline-block">Date: </label>
<input type="text" id="datepicker" name="date" class="form-control" value=""></input><br>
<p>Are you already a member of the youth?</p>
<label for=member>
<input type="radio" name="member" value="Yes member">Yes
<input type="radio" name="member" value="Non member">No
</select><br><br>
<input class="btn btn-dark" id="sendbtn" type="submit" name="send" value="Send">
</form>
I am stumped....
I have created a PHP form to gather some user information and email it to the site owner, after the form is submitted, the fields auto complete with a '1' I am assuming this means the field was true and submitted.
Does anyone have any suggestions on how to hide or remove the 1 after the form is submitted? I just want the form to be blank after submission. Here is the code, Sorry for the poor formatting and eye bleeding wall of code.
Thank you in advance for any help!
PHP Code
$errName = "";
$errcatBreed = "";
$errEmail = "";
$errMessage = "";
$result = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$catBreed = $_POST['catBreed'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'PearTreeHill Contact Form';
$to = 'test#domain.com';
$subject = "New furbaby enquiry from $name";
$body ="From: $name\n Cat Breed: $catBreed\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if cat breed has been entered
if (!$_POST['catBreed']) {
$errcatBreed = 'Please enter either Ragdoll or British ShortHair';
}
// 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['message']) {
$errMessage = 'Please enter your message';
}
// RECAPTCHA
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
$secret = 'Private Key';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success)
{
$succMsg = 'Your contact request have submitted successfully.';
}
else
{
$errMsg = 'Robot verification failed, please try again.';
}
}
// If there are no errors, send the email
if (!$errName && !$errcatBreed && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
HTML Code
<!-- contact form -->
<section id="contactUs" class="bg">
<div class="container">
<div class="row">
<div class="col-lg-8 mx-auto">
<div class="text-center">
<h2 class="w3-tangerine">Contact Us</h2>
</div>
<!-- start of entry form -->
<form class="form-horizontal" role="form" method="post" action="index.php">
<!-- name entry -->
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars(isset($_POST['name'])); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<!-- cat breed selection -->
<div class="form-group">
<label for="catBreed" class="col-sm-2 control-label">Cat Breed</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="catBreed" name="catBreed" placeholder="'Ragdoll' or 'British ShortHair'" value="<?php echo htmlspecialchars(isset($_POST['catBreed'])); ?>">
<?php echo "<p class='text-danger'>$errcatBreed</p>";?>
</div>
</div>
<!-- email address entry -->
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-12">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars(isset($_POST['email'])); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<!-- Body of message -->
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-12">
<textarea class="form-control" rows="4" name="message" placeholder="Please enter any other information"><?php echo htmlspecialchars(isset($_POST['message']));?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<!-- reCAPTCHA -->
<div class="g-recaptcha" data-sitekey="Public Key" data-callback="recaptcha_callback"></div>
<!-- send button -->
<input disabled="disabled" id="submit" name="submit" type="submit" value="Send Message" class="btn btn-danger">
<label for="submit" class="col-sm-8 control-label">Please allow up to 48 hours for a response!</label>
</div>
</div>
<!-- entry alert -->
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
<?php echo htmlspecialchars(isset($_POST['email'])); ?>
Here you display the result of isset() (TRUE or FALSE).
You should do :
<?php if ( isset($_POST['email']) ) echo htmlspecialchars($_POST['email']); ?>
And this for each field :-)
change
echo htmlspecialchars(isset($_POST['name']));
to
echo htmlspecialchars ($_POST['name'])
I mean remove isset();
<?php
function goback()
{
header("refresh:1; url=index.php");
exit;
}
if(isset($_POST['submit'])){
$name = $_POST['name'];
$comment = $_POST['comment'];
$mob = $_POST['mob'];
$email = $_POST['email'];
$to ='xyz#gmail.com';
$subject= 'Contact form pharma';
$message ="Name: ".$name."\n".
"Comment: ".$comment."\n".
"Mobile: ".$mob."\n".
"E-mail: ".$email;
if(mail($to, $subject, $message)){
echo "<script type='text/javascript'>alert('message successfully sent');window.close();</script>";
goback();
}
else
{
echo "something went wrong";
}
}
?>
Index.php on this page we have a form request acallback for which we use a html form and mail Function
<form>
<fieldset>
<legend>
<center>Request a call back</center>
</legend>
<div class="form-group">
<label class="control-label" for="requestid">Your Name</label>
<div>
<input id="requestid" name="name" placeholder="Name please" class="form-control input-md" required="" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label" for="dis">Your Comment</label>
<div>
<textarea class="form-control" placeholder="Your Comment" id="comment" name="comment"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label" for="dis">Mobile No</label>
<div>
<input placeholder="1234-456-7890" class="form-control input-md" id="phonenum" name="mob" type="tel" required >
</div>
</div>
<div class="form-group">
<label class="control-label" for="dis">E-mail</label>
<div>
<input placeholder="xyz#example.com" class="form-control input-md" id="email" name="email" type="email" required >
</div>
</div>
<div class="form-group">
<div class="text-right">
<button id="submit" name="submit" class="btn btn-info">Submit Message</button>
</div>
</div>
</fieldset>
</form>
I want to display this alert box on the same page. It get redirect to blank page then shows alert box . If goback() removed then it redirects to blank page and does no return back to previous page.
how to solve this?
You are missing something. How it can be possible to display a popup related to another page without sending any request to it? First I have set a success session in mail.php page which is set if mail is sent successfully.
<?php
session_start();
function goback()
{
$_SESSION['success'] = 'true';
header("location:index.php");
}
if(isset($_POST['submit'])){
$name = $_POST['name'];
$comment = $_POST['comment'];
$mob = $_POST['mob'];
$email = $_POST['email'];
$to ='xyz#gmail.com';
$subject= 'Contact form pharma';
$message ="Name: ".$name."\n".
"Comment: ".$comment."\n".
"Mobile: ".$mob."\n".
"E-mail: ".$email;
if(mail($to, $subject, $message)){
goback();
}
else
{
echo "something went wrong";
}
}
?>
Then in index.php page we just have to call this session. if its true then we can show pupop
<?php
if(isset($_SESSION['success']) && $_SESSION['success']=='true')
{
//show pupop
$_SESSION['success'] = 'false';
}
?>
This is the code for the form.
<?php
if ($_POST) {
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$checkbox = '';
if (isset($_POST['checkbox'])) {
$checkbox = 'Yes';} else{
$checkbox = 'No' ;}
$message = $_POST['message'];
$from = 'Demo Contact Form';
$to = 'imvael#gmail.com, vnikolic1#cps.edu';
$subject = 'Message from Contact Demo ';
$body ="From: $name\n E-Mail: $email\n Company: $company\n Phone: $phone\n Opt In?: $checkbox\n Message:\n $message";
$headers = 'From: webmaster#bradfordsystems.com' . "\r\n" .
'Reply-To: ' .$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Check if name has been entered
if (!$_POST['name']) {
$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 name has been entered
if (!$_POST['phone']) {
$errName = 'Please enter your phone number';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// then after the posting of the form data
// validation
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && isset($_POST['url']) && $_POST['url'] == '') {
if (mail ($to, $subject, $body, $headers)) {
header("Location: thankyou.php"); /* Redirect browser */
exit();
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
This is the form itself
<div class="wrapper">
<div class="row">
<div class="col _66">
<?php echo $result; ?>
<form role="form" name="contactForm" id="contactForm" method="post" action="contact.php#contactForm">
<p>
We welcome your feedback and inquiries. Please use the form below to get in touch.
</p>
<div class="row">
<div class="col">
<input type="text" id="name" name="name" placeholder="Name" value="<?php echo htmlspecialchars($_POST['name']); ?>" required>
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="col">
<input type="email" id="email" name="email" placeholder="Company Email" value="<?php echo htmlspecialchars($_POST['email']); ?>" required>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="row">
<div class="col">
<input type="text" id="company" name="company" placeholder="Company" value="<?php echo htmlspecialchars($_POST['company']); ?>" required>
</div>
<div class="col">
<input type="tel" id="phone" name="phone" <?php echo htmlspecialchars($_POST['phone']); ?> placeholder="Phone" required>
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="row">
<div class="col">
<input type="number" id="zipcode" name="zipcode" placeholder="Zip Code" value="<?php echo htmlspecialchars($_POST['zipcode']); ?>">
</div>
<div class="col">
<input id="checkBox" type="checkbox"> <span id="optInText">YES, I want a Free Workspace Evaluation!</span>
</div>
</div>
<div class="row">
<div class="col">
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
</div>
</div>
<div class="row">
<div class="col submit-col">
<p>Questions or Comments?</p>
<textarea id="message" name="message" placeholder="Enter your questions or comments here" style="height:200px"> <?php echo htmlspecialchars($_POST['message']); ?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
<button class="btn btn-dark hvr-underline-from-left" name="submit" type="submit" value="Send">Submit Request</button>
</div>
</div>
</form>
</div>
</div>
</section>
I am writing a contact form script from scratch The code is working correctly, but I am unable to prevent resubmissions if they user refreshed the page or press the submit buton more then once.
Also is it a bad practice to use self submitting contact forms?
I am trying to create a very simple contact form for my personal website. This form worked perfect for another website I did so I'm not sure what the problem is here. I've spent the last few hours trying to figure it out and dipping into other form tutorials so its strayed from the original code. I guess the biggest difference between this and the other website is that this is in bootstrap.
I have three problems with this.
1) Is there anyway not to jump up to the top of the screen when I submit the form? I believe this has something for making the action the page itself.
2) Is there any way to not need the 'subject' variable for the mail function? I'd love to not have the subject input on my form.
3) The biggest problem is that while the form runs, I have not received any emails from the form.
The php before html on same doc:
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 't*************#gmail.com';
$subject = $_POST['subject'];
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$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['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I 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>';
}
}
}
?>
The html/form:
<form class="form-horizontal" role="form" method="post"
action="pauline.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name"
placeholder="First & Last Name" value="<?php echo
htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email"
name="email" placeholder="example#domain.com" value="<?php echo
htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-
label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject"
name="subject" placeholder="Message Subject" value="<?php echo
htmlspecialchars($_POST['subject']); ?>">
<?php echo "<p class='text-danger'>$errSubject</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-
label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?php
echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?
</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human"
name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send"
class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
I have your issues resolved with example code. See code comments for exact edits
This is just a matter of setting an anchor to jump the back page
down on load
Just set the subject to a blank, and not
have any html for the subject
This was fixed on its own by cleaning up your undefined errors. Note
the issets() I used with shorthand if statements $if?$then:$else to define the variables as blank if there is nothing coming
from $_POST
I recommend you take care of your format needs on top to keep your code as organized as you can. Also use the variables you defined as opposed to calling $_POST repeatedly like your example.
<?
$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ""; ### shorthand if statements
$email = isset($_POST['email']) ? htmlspecialchars($_POST['email']) : "";
$message = isset($_POST['message']) ? htmlspecialchars($_POST['message']) : "";
$human = isset($_POST['human']) ? intval($_POST['human']) : -1;
$from = 'Contact Form';
$to = 't*************#gmail.com';
$subject = ""; ### send a blank subject. issue 2 solved
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$errName = $errEmail = $errMessage = $errHuman = $result = ""; ### defining these all as blank to stop the undefined error.
if (isset($_POST["submit"])) {
// Check if name has been entered
if (!$name) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$message) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail($to, $subject, $body, $from)) {
$result = '<div class="alert alert-success">Thank You! I 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>';
}
}
}
?>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form class="form-horizontal" role="form" method="post"
action="pauline.php#submit"> <!-- // this solves issue 1 -->
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name"
placeholder="First & Last Name" value="<?php echo $name; ?>">
<?php echo "<p class='text-danger'>$errName</p>"; ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email"
name="email" placeholder="example#domain.com" value="<?php echo $email; ?>">
<?php echo "<p class='text-danger'>$errEmail</p>"; ?>
</div>
</div>
<!--// BYE BYE SUBJECT!
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject"
name="subject" placeholder="Message Subject" value="<?php # echo htmlspecialchars($_POST['subject']); ?>">
<?php # echo "<p class='text-danger'>$errSubject</p>"; ?>
</div>
</div>
-->
<div class="form-group">
<label for="message" class="col-sm-2 control-
label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message">
<?php echo $message; ?>
</textarea>
<?php echo "<p class='text-danger'>$errMessage</p>"; ?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?
</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human"
name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>"; ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send"
class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</body>
</html>
1) Is there anyway not to jump up to the top of the screen when I submit the form? I believe this has something for making the action the page itself.
Because you're posting to the page and not providing a redirect, you'll always refresh the page, and you'll always reload the page at the top first. You could provide a Javascript function to scroll to a certain point, or redirect to an anchor tag on the page (though this would involve an unnecessary reload the way you're running it).
2) Is there any way to not need the 'subject' variable for the mail function? I'd love to not have the subject input on my form.
Sure! You can hardcode a subject into your PHP. Just replace your $subject = $_POST['subject']; with $subject = 'Message From Site'. Then remove the input from your form. You've already done this with your $from variable.
3) The biggest problem is that while the form runs, I have not received any emails from the form.
This is a more difficult issue. Emails from PHP scripts can be difficult to get through email provider spam filters. You may need to set some configuration or set specific headers in order to get emails sent to you.The mail() function accepts a set of headers as the fourth parameter, not just a "From" value - tweaking that may help. You can find TONS more info on debugging options aon the PHP docs, located here: http://php.net/manual/en/function.mail.php