i just load the page yet the error has been displayed
<?php
$nameErr = $emailErr = $idErr = $passwordErr = "";
$name = $email = $id = $password = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["id"])) {
$genderErr = "Matric number is required";
} else {
$website = test_input($_POST["id"]);
if (!preg_match("/^[0-9].*$/",$id)){
$idErr = "Only numbers allowed"; }
}
}
if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} else {
$password = test_input($_POST["password"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z0-9]+$/",$password)) {
$nameErr = "Special characters are not allowed";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Pendaftaran Kali Pertama</title>
<style type="text/css">
<!--
.style1 {
color: #FFCC00;
font-weight: bold;
}
.error{ color:#FFCC00}
-->
</style>
</head>
<body>
<div align="center">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table width="62%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td height="35" colspan="4" bgcolor="#660066"><div align="center" class="style1">PENDAFTARAN KALI PERTAMA </div></td>
</tr>
<tr>
<td width="33%"><div align="right"><strong>Nama </strong></div></td>
<td width="5%"><div align="center"><strong>:</strong></div></td>
<td width="62%"><input name="name" type="text" size="50" /><span class="error">* <?php echo $nameErr;?></span></td>
</tr>
<tr>
<td><div align="right"><strong>No. Pelajar </strong></div></td>
<td><div align="center"><strong>:</strong></div></td>
<td><input name="id" type="text" size="50" maxlength="10" /><span class="error">* <?php echo $idErr;?></span></td>
</tr>
<tr>
<td><div align="right"><strong>Kata Laluan(8 aksara sahaja)</strong></div></td>
<td><div align="center"><strong>:</strong></div></td>
<td><input name="password" type="text" size="50" maxlength="8" /><span class="error">* <?php echo $passwordErr;?></span></td>
</tr>
<tr>
<td><div align="right"><strong>Emel</strong></div></td>
<td><div align="center"><strong>:</strong></div></td>
<td><input name="email" type="text" size="50" /><span class="error">* <?php echo $emailErr;?></span></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
it exactly no error, but when i load the page(the submit button is not even click) the password error has been displayed. other field error work just fine.
Your password block is outside of the POST block and that is why the
$passwordErr
is always set even before the form is submitted.Put it inside the block
if ($_SERVER["REQUEST_METHOD"] == "POST") {
Also in your code
$nameErr = "Special characters are not allowed";
is wrong. You must assign the error to $passwordErr like I have done in the code above.