Hi all,
I have recently finished my website and included a contact form so that visitors have the option to send me a message. I've followed a thread here: https://bootstrapstudio.io/forums/topic/how-to-make-functional-my-contact-form/ and got the php file to work on submit but I am running into two problems:
1) The emails do not reach my inbox (or they aren't sent in the first place...) - I did check the server error log but there appear to be no errors.
2) When submitting the message it loads the php file and shows the success message on a different page instead of staying on the same page.
The code HTML code I am using is:
<div id="contact" class="contact-clean">
<form method="post" action="contact.php">
<h2 class="text-center" id="heading-contact-h2">Contact</h2>
<div class="d-flex mx-auto" id="contact-line-divider"></div>
<div class="form-group"><input class="form-control" type="text" name="name" required placeholder="Naam" /></div>
<div class="form-group"><input class="form-control is-invalid" type="email" name="email" required placeholder="Email" /></div>
<div class="form-group"><textarea rows="15" name="message" placeholder="Bericht" class="form-control" required style="height:150px;"></textarea></div>
<div class="form-group text-center"><button class="btn btn-primary" type="submit" id="contact-form-button" name="send">Verstuur</button></div>
</form>
</div>
The PHP code I am using is:
<?php
if (isset($_POST["send"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Website Contact Form';
$to = 'info@portrayalmarketing.com';
$subject = 'Message from Contact Form ';
$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'; }
// 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>';
}
}
} ?>
Thank you in advance for the help!