我有一个包含几个字段的表单。我想为每个缺失的字段显示不同的错误消息。如果一个字段为空,我会显示一个jQuery对话框和要正确填写的初始表单。
我做过这样的事情:
if(isset($_POST['submit'])){
if($_POST['email'] && $_POST['email'] != ''){
if($_POST['password'] && $_POST['password']!= ''){
if($_POST['password'] == $_POST['confirm_password'])
{
...
...
}
}else ?> <div class="dialog" id="dialog" title="Error"> <p>The passwords you entered do not match</p> </div><?php
}else ?> <div class="dialog" id="dialog" title="Error"> <p>Password field is empty</p> </div><?php
}else ?> <div class="dialog" id="dialog" title="Error"> <p>Email field is empty</p> </div><?php
}else {?>
然后我显示我的表单
<form method="post" action="submenu_signup.php">
<table class="center">
<tr>
<td width="50%" > Email : </td>
<td width="50%" > <input class="more_width" type="text" name="email" id="email" placeholder="Ex : Surgeon5031@example.com" maxlength="45" /></td>
</tr>
<tr>
<td width="50%" > Password : </td>
<td width="50%" > <input class="more_width" type="password" name="password" id="password" maxlength="40" /></td>
</tr>
<tr>
<td width="50%" > Confirm password : </td>
<td width="50%" > <input class="more_width" type="password" name="confirm_password" id="confirm_password" maxlength="40" /></td>
</tr>
<tr>
<td width="50%" > Service : </td>
<td width="50%" > <input class="more_width" type="text" name="service" id="service" placeholder="Ex : Neurosurgery, orthopedics, ..." maxlength="255" /></td>
</tr>
<tr>
<td width="50%" > Phone number : </td>
<td width="50%" > <input class="more_width" type="text" name="phone_number" id="phone_number" placeholder="Ex : 01234 56 78 90" maxlength="20" /></td>
</tr>
<tr>
<td colspan="2" > <input class="submit" type="submit" name="submit" id="submit" value="Sign up" /> </td>
</tr>
</table>
</form>
但很明显,当用户没有正确填写时,它会显示jQuery框,但表单已不存在。我不会
CTRL+CTRL+V
每次后我的表格
其他
我确信我做得不好,但不知道最好的做法。我对每一个建议都持开放态度(我不在乎用其他东西取代jQuery)
有几个答案。我要做的是将错误消息存储在一个数组中,并将字段名称作为关键字。然后,您可以在字段下显示每个错误消息(如以下示例所示),也可以选择在第一个大错误<div>
中显示所有内容,然后在字段上添加一些错误class
(例如,它们将显示为红色)。
你有一个数组,每个字段都有一个错误,还有消息,这很方便,你可以随心所欲地使用它。
<?php
$errorMessages = array();
if (isset($_POST['submit'])) {
if (empty($_POST['email']) {
$errorMessages['email'] = 'Email field is empty';
}
if (empty($_POST['password'] || $_POST['password'] != $_POST['confirm_password']) {
$errorMessages['password'] = 'The passwords you entered do not match';
}
if ( ...other error... ) {
$errorMessages[{fieldName}] = '{message}';
}
if (empty($errorMessages)) {
// No error : do what you have to do with your datas
}
?>
<form method="post" action="submenu_signup.php">
<table class="center">
<tr>
<td width="50%" ><label for="email">Email :</label></td>
<td width="50%" >
<input class="more_width" type="text" name="email" id="email" placeholder="Ex : Surgeon5031@example.com" maxlength="45" />
<?php if (!empty($errorMessages['email'])) echo $errorMessages['email']; ?>
</td>
</tr>
<tr>
<td width="50%" ><label for="password">Password :</label></td>
<td width="50%" >
<input class="more_width" type="password" name="password" id="password" maxlength="40" />
<?php if (!empty($errorMessages['password'])) echo $errorMessages['password']; ?>
</td>
</tr>
<tr> ... other fields ... </tr>
</table>
</form>
(注意:我不确定在这里使用HTML tables
是否是一个好的做法。对表使用table
。如果不是table
,请使用div
和p
以及其他适当的HTML标记。)
您也可以在用PHP提交之前使用Javascript验证。它可以更用户友好,但你总是必须进行PHP验证,以防用户关闭Javascript(如果你想"破解"表单,很容易使用它)。