0

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>
  • `my code just stopped working` what does this mean ? – Nandan Bhat May 09 '17 at 13:19
  • Sorry I guess that isn't very specific. Basically, I have an error ---> else { $error = ""; } and that's all I get back, I can't seem to find any way to troubleshoot it and have no idea what's wrong, it was working perfectly fine and then just started coming back negative, it's even decided to work once or twice too, it's not the actual email (though that's not working either) but the actual process that comes back as no – Ryan Konecny May 09 '17 at 15:02
  • Use `error_get_last()` 1. First turn on error reporting by `error_reporting(E_ALL);` 2. `error_get_last())` in else block. and exit(); the script after this. So that you can only see the errors. I hope that works :) – Nandan Bhat May 10 '17 at 06:42
  • Having come back to try again today it's all started working, though I'm sure it will inevetably fail again and when it does I will definitely give that a shot, thank you so much :) – Ryan Konecny May 11 '17 at 11:54

0 Answers0