I have a simple form on my website that I need to do server side validation on. If user enters incorrect data or leaves a field blank, the user should be re-shown the form with their submitted data pre-filled into the fields.
My form code looks like this
<!--Contact form-->
        <form name="contactform" action="validate.php" method="post">
            Name: 
            <input type="text" name="name" value=""><br>
            <span class="error">* <?php echo $nameError; ?></span><br>
            Email: 
            <input type="text" name="email" value="">
            <span class="error">* <?php echo $emailError; ?></span><br>
            Category:
            <select name="category">
                <option value="" disabled selected>Select</option>
                <option value="shipping">Sales</option>
                <option value="returns">Returns</option>
                <option value="shipping">Shipping</option>
                <option value="other">Other</option>
            </select><br>
            <textarea rows="10" cols="40" name="message" placeholder = "Send us a message"></textarea><br>
            <input type="submit" name="submit" value="Submit" onclick="validate()">
        </form>
At the moment all I'm testing to validate is the name and email fields. My problem is that when I load the page I immediately get errors saying "Notice: Undefined variable: nameError " which has to do with these lines in the code
<span class="error">* <?php echo $nameError; ?></span><br>  
<span class="error">* <?php echo $emailError; ?></span><br>
However I know the validations are working because I placed echo statements in my validate.php file to test this. If I submit the form with incorrect or correct data, it will take me to a blank validate.php page that just displays the echo statements
Here is my code for validate.php
## Initialise varialbes to null ##
$nameError ="";
$emailError ="";
$categoryError ="";
$messageError ="";
$name = $_POST['name'];
## On submitting form below function will execute ##
if(isset($_POST['submit'])){
if (empty($_POST["name"])) {
    $nameError = "Name is required";
    echo "name required";
    echo " ";
} else {
    //$name = test_input($_POST["name"]);
    //check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
        $nameError ="Only letters and white space allowed";
        echo "Invalid name";
    }
}
if(empty($_POST["email"])) {
    $emailError = "Email is required";
    echo "email required";
} else {
    //$email = test_input($_POST["email"]);
    //check if email syntax is valid or not
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]/", $email)) {
        $emailError = "You did not enter a vaild email";
        echo "invalid email";
    }
}
}
?>
So I know my validations are working, the problem is that they are not displaying on the form itself. Can anyone offer a solution on how to fix this? I've included a picture of one of the error messages I get when I load up the page Error message on page load
 
    