I have a contact form in which I am using PHP to complete the server side of things.
The issue I am encountering is that the form is submitting when the submit button is clicked but it is not displaying the form, the error message(s), or the success message.
When I reload the page, the form and the appropriate messages(s) appear.
I cannot see any clear reason as to why this would be so any help would great!
<?php
// $error and $success variable set to blank
$error = "";
$successMessage = "";
// check to see if any $_POST variables have been entered
if ($_POST) {
// if $_POST variable ["email"] has no value
if (!$_POST["email"]) {
// $error variable to add comment advising that data is missing
$error .= "An email address is required!!!!!!!!<br>";
}
if (!$_POST["subject"]) {
$error .= "The subject field is required!!!!<br>";
}
if (!$_POST["content"]) {
$error .= "The content field is required!!!!!<br>";
}
// IF $error variable is no longer empty
if ($error != "") {
// $error variable to genereate html + $error
$error = '<div><p><strong>Whoops! There were error(s) in the form:</strong></p>' . $error . '</div>';
} else {
$emailTo = "email@example.com";
$subject = $_POST["subject"];
$content = $_POST["content"];
$headers = "From: ".$_POST["email"];
// IF all email variables have had values assigned then $successMessage to display html sucess message
if (mail($emailTo, $subject, $content, $headers)) {
$successMessage = '<div>Thank you for your enquiry, we will get back to you shortly!</div>';
// ELSE email variable(s) are missing a value - $error variable to display html error message
} else {
$error = '<div>Unfortuantely your enquiry could not be sent, please try again later</div>';
}
}
}
?>
<html>
<div id="contactOuter">
<div id="contactInner">
<a href="javascript:void(0);"><span id="close">×</span></a>
<h1>Get in touch</h1>
<div><? echo $error.$successMessage;?></div>
<form method="post">
<label for="email">Email address:</label><br>
<input name="email" type="email" placeholder="Enter email" id="email">
<label for="subject">Subject:</label>
<input name="subject" type="text" id="subject">
<label for="content">What would you like to ask us?</label><br>
<textarea name="content" rows="7" id="content"></textarea>
<button type="submit" id="submit">Submit</button>
</form>
</div>
</div>
</html>