if($_POST){
$errors = array();
//start validation
if(empty($_POST['email'])){
    $errors['email1'] = "Your Email Cannot be empty"; 
}
if (!filter_var(['email'], FILTER_VALIDATE_EMAIL)){
    $errors['email2'] = "Email not valid!";
}
if(empty($_POST['phone'])){
    $errors['phone1'] = "Your Number Cannot be empty"; 
}
if(strlen($_POST['phone']) < 8){
    $errors['phone2'] = "Your Phone Number must be at least 8 characters long";
}
if(strlen($_POST['phone']) > 8){
    $errors['phone3'] = "Your Phone Number must be not more than 8 characters long";
}
//check errors
if(count($errors) == 0)
{
    //redirect to home page
    header("Location: index.php");
    exit();
}
}
This is my code which needs to be validated. The validation works ok but here is the problem.
<div class="form-group">
            <label for="name">Email</label>
            <input type="email" class ="form-control" name="email" id="email" placeholder="Enter Email">
            <p><?php if(isset($errors['email1'])) echo $errors['email1'];?></p>
            <p><?php if(isset($errors['email2'])) echo $errors['email12'];?></p>
        </div>
        <div class="form-group">
            <label for="phone">Phone Number</label>
            <input type="text" class ="form-control" name="phone" id="phone" placeholder="Enter Phone Number">
            <p><?php if(isset($errors['phone1'])) echo $errors['phone1'];?></p>
            <p><?php if(isset($errors['phone2'])) echo $errors['phone2'];?></p>
            <p><?php if(isset($errors['phone3'])) echo $errors['phone3'];?></p>
        </div>
I have the form set to save to a file. The inputs will be validated and the error messages will be displayed but the form that has wrong inputs will still be saved to a file. Any help would be appreciated thanks.
<?php
    if(isset($_POST['email'])  && isset($_POST['phone'])  && isset($_POST['enquiry'])){
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $enquiry = $_POST['enquiry'];
        $file = "form.txt";
        $save = "Email: ".$email."\n".
                "Phone: ".$phone."\n".
                "Comment: ".$enquiry."\n";
        file_put_contents($file,$save, FILE_APPEND);
}
?>
 
    