So, I'm following a course and have successfully sent myself multiple emails using php mail. The next step was validation of each input, first through jquery and then php. But reaching the end of the validations my code just stopped working. I've hit ctrl+Z straight back to places it worked with no success and on coming back to the current point it decided to work once and then go straight back to nothing again.
I don't want to miss anything here and I'd rather better my understanding and push through this than give up and move onto the next thing, any help would be much appreciated.
Here's my code, happy to provide any other information if needed. Also upon setting this code to the final state again and refreshing it decided to work once and then not straight after on my excited second try. I'm insanely confused.
Ps. This isn't just about the email sending and being received, the entire thing comes back false, please don't write this off as the same as a different question and mark it as a duplicate. I've been through enough of them to know I needed to ask this seperately.
<?php
$error = ""; $success = "";
if ($_POST) {
if (!$_POST["email"]) {
$error .= "An email adress is required.<br>";
}
if (!$_POST["subject"]) {
$error .= "A subject is required.<br>";
}
if (!$_POST["content"]) {
$error .= "The content field is required.<br>";
}
if ($_POST["email"] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "The email address is invalid.<br>";
}
if ($error != "") {
$error = "<div class='alert alert-danger container' role='alert'><strong>There were error(s) in your form:</strong><br>" . $error . "</div>";
} else {
$emailTo = "ryanmikekonecny@hotmail.com";
$subject = $_POST['subject'];
$content = $_POST['content'];
$headers = "From: ".$_POST['email'];
if (mail($emailTo, $subject, $content, $headers)) {
$success = "<div style='margin-top: 10px;margin-bottom: 20px;' class='alert alert-success container' role='alert'>Email sent successfully, we will be in touch soon!</div>";
} else {
$error = "<div style='margin-top: 10px;margin-bottom: 20px;' class='alert alert-danger container' role='alert'>Sorry something seems to be wrong, bear with us!</div>";
}
}
}
;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.min.css">
</head>
<body data-spy="scroll" data-target="#navScroll" data-offset="150">
<h1 class='container'>Get in touch!</h1>
<div id="error"><? echo $error.$success; ?></div>
<form class="form-group container" method="post">
<h3>Email address</h3>
<input class="form-control mr-sm-2 logIn" type="email" placeholder="Enter email" size="14" name="email" id="email">
<h3>Subject</h3>
<input class="form-control mr-sm-2 logIn" type="text" placeholder="" size="14" name="subject" id="subject">
<h3>What would you like to ask us?</h3>
<input class="form-control mr-sm-2 logIn" type="text" placeholder="" size="14" name="content" id="content">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Submit</button>
</form>
<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script type="text/javascript" src="script.js">
</script>
</body>
</html>