有效电子邮件仍然返回false

So I'm trying to check if an email is valid, I know I can only use JS for this, but I prefer my way. So here's what I'm doing

//Get data
if(isset($_POST['email'])){
    $email = htmlspecialchars($_POST['email'], ENT_QUOTES, 'UTF-8')
}
else{
    header('Location: /');
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    echo 1;
}
else{
    echo 0;
}

And my JS

    $(document).ready(function() {
    //listens for typing on the desired field
    $("#email").keyup(function() {
        //gets the value of the field
        var email = $("#email").val(); 

        //here is where you send the desired data to the PHP file using ajax
        $.post("../classes/validate.php", {email:email},
            function(result) {
                if(result == 1) {
                    //Email is valid
                    console.log("Good");
                }
                else {
                    //Email is invalid
                    console.log("Bad");
                }
            });
     });
});

Now no matter what I enter, it's always returning Bad. Even with a valid email. Any ideas?

You're missing a semicolon right after you store email as a variable.

//Get data
if(isset($_POST['email'])){
    $email = htmlspecialchars($_POST['email'], ENT_QUOTES, 'UTF-8');
}
else{
    header('Location: /');
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
    echo 1;
}
else{
    echo 0;
}

You should not be using htmlspecialchars() before validation. Use it only for presentation ie. when outputting the email in HTML.