Hullo. I am trying to make a modal appear on my website that will let people who visit send messages to an email i have designated. I am completely new to coding and have spent many hours trawling these forums for help. I followed this tutorial: https://bootstrapbay.com/blog/working-bootstrap-contact-form/.
This is my html:
<div class ="modal fade" id="contact" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal" role="form" method="post" action="contact.php">
<div class="modal-header">
<h4> Contact </h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="contact-name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input name="nameinput" type="text" class="form-control" id="contact-name" placeholder="First & Last Name">
<?php echo "<p class='text-danger'> $errName </p>";?>
</div>
</div>
</div>
<div class="modal-body">
<div class="form-group">
<label for="contact-email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input name="emailinput" type="email" class="form-control" id="contact-email" placeholder="example@gmail.com">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
</div>
<div class="modal-body">
<div class="form-group">
<label for="contact-message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea name = "messageinput" class="form-control" id="contact-message" rows="4"></textarea>
<?php echo "<p class='text-danger'>$errMsg</p>";?>
</div>
</div>
</div>
<div class="modal-body">
<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="Type your answer here">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
</div>
<div class="modal-body">
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<!-- will be used to display an alert to the user -->
</div>
</div>
</div>
<div class="modal-footer">
<div class = "form-group">
<div class="col-sm-10 col-sm-offset-2">
<a class="btn btn-default" data-dismiss="modal">Close</a>
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
This is my php:
<?php
$errName='' ;
$errEmail='';
$errMsg='';
$result='';
$errHuman='';
//have to define variables outside of if statements, even if you just set them to null or ''//
if(isset($_POST["submit"])) {
$name = $_POST['nameinput'];
$email = $_POST['emailinput'];
$message = $_POST['messageinput'];
//$human = intval($_POST['human']);
$from = 'From: $name';
$to = 'danroebuck1994test@gmail.com';
$subject = 'Message from Contact Form';
$body ="From: $name
E-Mail: $email
Message:
$message";
// Check if name has been entered
if (!$_POST['nameinput']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['emailinput'] || !filter_var($_POST['emailinput'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['messageinput']) {
$errMsg = 'Please enter your message';
}
//If there are no errors, send the email
if (!$errName && !$errEmail && !$errMsg) {
if (mail ($to, $subject, $body, $from)) {
echo "<script type='text/javascript'>alert('Email sent successfully')</script>";
} else {
echo "<script type='text/javascript'>alert ('An error occurred, the email has not been sent. Please try again later.')</script>";
}
//}
}
?>
The first trouble I am having is that when I open up the modal by pressing the contact button, the red text is not displaying below the fields to ask for the required inputs. The second issue is that whenever I refresh the page, the previous email I set up in the form is sent lol, with the alert popping up saying 'your email has been sent successfully'. This seemed very weird to me as I thought using the if(isset($_POST["submit"])) would mean it would only be sent if the submit button was clicked.
I ask for any assistance possible. I did search these forums for a solution and I found this: PHP email form sends email everytime page is refreshed. However I did not find it helpful to me. As this was 6 hours ago and I have now made no progress I figured it would be reasonable to ask. Thankyou friends.
</div>