I have this form element in my php page, and i want to echo out the error message underneath the form element.
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" >
Name: <input type="text" name="username"><br>
<span><?php echo nameErr; ?></span>
password: <input type="password" name="password"><br>
<span><?php echo passErr; ?></span>
Gender:<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<span><?php echo genderErr; ?></span>
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(empty($_POST['username'])) {
$nameErr = 'name can\'t be empty';
else{
$username = $_POST['username'];
}
if(empty($_POST['password'])) {
$passErr = 'Password can\'t be empty ';
else{
$password = $_POST['password'];
}
if(empty($_POST['gender'])) {
$genderErr = 'Gender cant be empty';
}else{
$gender = $_POST['gender'];
}
}
}
?>
The major problem is that when i the page is ran, it show error messages that variable $nameErr is not defined, $passErr is not defined, and $genderErr is not defined, My question is that how can i actually notified users of error messages where necessary and it should be close to the form element too.