if语句变量为空时如何显示错误

I feel as if im super close but can only seem to display a message below rather then where i set my span. I have a feeling its because my if ( isset ) is below the form but i cant seem to put all the php on top as it wont display anything because of the submit function.. I know its possible to set a variable from an if statement however because its below the form it doesn't seem to work.. surely there is a way around this? I can't seem to find anything online with a similar situation. Here is the code im working with...

        <form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
                <div class="form-group">
                <label>Product Name</label>
                <input type="text" class="form-control" id="productName" name="productName" placeholder="Enter Product Name"><span class="error"><?php echo $nameError;?></span>
                </div>
                <div class="form-group">
                <label>Quantity</label>
                <input type="Number" class="form-control" id="Quantity" name="QTY" placeholder="Enter Quantity" >
                </div>
                <div class="form-group">
                <label>Price</label>
                <input type="text" class="form-control" id="productPrice" name="productPrice" placeholder="Enter Price">
                </div>
                <div class="form-group">
                <label>Variable</label>
                <input type="text" class="form-control" id="variable" name="Variable" placeholder="Enter Variable">
                </div>
                <div class="form-group">
                <label>Description</label>
                <textarea class="form-control" id="productDescription" name="productDescription"/></textarea>
                </div>

                <input type="submit" name="submit" value="Submit" class="btn btn-primary"></input>

        </form>
        <?php
        if(isset($_POST['submit'])){

                if(empty($productName) || empty($productPrice))
                {
                echo "All * fields are required.";
                }
                else {

        $productName = $_POST["productName"];
        $productDescription = $_POST['productDescription'];
        $productPrice = $_POST['productPrice'];
        $QTY = $_POST['QTY'];
        $Variable = $_POST['Variable'];


            $sql = "INSERT INTO product (productName, productDescription, productPrice, QTY, Variable) VALUES (:productName, :productDescription, :productPrice, :QTY, :Variable)";
            $statement = $conn->prepare($sql);
            $statement->bindValue(':productName', $productName);
            $statement->bindValue(':productDescription', $productDescription);
            $statement->bindValue(':productPrice', $productPrice);
            $statement->bindValue(':QTY', $QTY);
            $statement->bindValue(':Variable', $Variable);
            $result = $statement->execute();
            $statement->closeCursor();


            header("location: ../view/success.php");
            return $result;
              }
            }
            ?>

Just try with this way:

<?php
    $nameErr = "";
    if (empty($_POST["productName"])) {
        $nameErr .= "productName is required";
    }
    if (empty($_POST["productDescription"])) {
        $nameErr .= "productDescription is required";
    }
    if (empty($_POST["productPrice"])) {
        $nameErr .= "productPrice is required";
    }
    if (empty($_POST["QTY"])) {
        $nameErr .= "QTY is required";
    }
    if (empty($_POST["Variable"])) {
        $nameErr .= "Variable is required";
    }   
    if($nameErr!=""){
        echo "All * fields are required.";
        echo $nameErr;
    }
    else{
        // Your process with SQL
    }

?>