如果用户未提交姓名、性别或宿舍,则在按下“注册”按钮时页面应显示红色错误。这是我的代码,但效果不佳。它总是给用户错误。
<?php
//checking if submitted
if(isset($_POST["action"]))
{
if (empty($_POST["name"]) || empty($_POST["gender"]) || empty($_POST["dorm"]))
$error=true;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fresh IMs</title>
</head>
<body>
<div style="text-align:center">
<h1>Register for Fresh IMs</h1>
<br/><br/>
<? if(isset($error)): ?>
<div style="color:red"> You must fill out the form!</div>
<? endif ?>
<form action="index.php" method="post">
<table style="border:0; margin-left:auto; margin-right:auto">
<tr>
<td>Name:</td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>Captain:</td>
<td><input name="captain" type="checkbox"></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input name="gender" type="radio" value="F">F
<input name="gender" type="radio" value="M">M
</td>
</tr>
<tr>
<td>Dorm:</td>
<td>
<select name="dorm">
<option value="" ></option>
<option value="Apply court" >Apply Court</option>
<option value="Candy">Candy</option>
</select>
</td>
</tr>
</table>
<input name="action" type="submit" value="Register">
</form>
</div>
</body>
</html>