I'm wanting my form to show an error such as "Enter first name" if the field has not being filled in. I have got to right code(I think) to do this but it is showing the error when the page is first loaded. I want it to be displayed when the form has been submitted.
<div class="Row">
<div class="Lable">First Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="firstname" class="detail" name="firstname" placeholder="First Name" />
</div> <!--End input-->
</div> <!--End row-->
<span class="error">* <?php echo $nameErr;?></span>
The PHP validation is
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
$nameErr = "Name is required";
} else {
$firstname = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
$nameErr = "Only letters and white space allowed";
}
}
Thanks