Php 管理空字段的最佳方法';s错误

Php 管理空字段的最佳方法';s错误,php,forms,Php,Forms,我有一个带有几个字段的表单。我想为每个缺少的字段显示不同的错误消息。如果一个字段为空,我将显示一个jQuery对话框和要正确填写的初始表单 我做过这样的事情: if(isset($_POST['submit'])){ if($_POST['email'] && $_POST['email'] != ''){ if($_POST['password'] && $_POST['password']!= ''){

我有一个带有几个字段的表单。我想为每个缺少的字段显示不同的错误消息。如果一个字段为空,我将显示一个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 {?>
if(isset($_POST['submit']){
如果($\u POST['email']&&$\u POST['email']!=''){
如果($\u POST['password']&&&$\u POST['password']!=''){
如果($\u POST['password']=$\u POST['confirm\u password']))
{               
...
...
}                       
}其他?>您输入的密码不匹配

密码字段为空

电子邮件字段为空

然后我显示我的表格

        <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+C CTRL+V

我的表格在每次

否则


我确信我做得不好,但不知道最好的做法是什么。我对每一个建议都持开放态度(我不在乎用其他东西取代jQuery)有几个答案。我要做的是将错误消息存储在一个数组中,并将字段名称作为键。然后,您可以在字段下显示每个错误消息(如以下示例所示),或者选择在第一个大错误
中显示所有内容,然后在字段上添加一些错误
(例如,它们将以红色显示)

您有一个数组,其中每个字段都有一个错误,并且包含一条消息,这非常方便,您可以根据需要使用它

<?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>

我认为,因为您将错误消息嵌套在
IF
语句中,这导致了问题。如果您将错误消息放在彼此之外,并且一个接一个地保存它们,那么它将处理每个错误消息,而不管前一个是真是假。但是,如果用户单击Submit by mistak,我不希望显示10个框e、 这不是很友好。我的目标是使这个页面变得柔软和用户友好。可能有一些javascript会帮助我在该字段没有目标时显示一些内容,你知道吗?你尝试过jquery验证吗?@Raccoon如果用户单击Submit,它将处理表单。你将如何确定这是否是一种行为是否有错误?将
移出最后一个
else
语句。这将显示表单以及错误消息(如果有)。表单未显示,因为表单提交后
isset($\u POST['submit'])
将为真,并且程序流不会进入
else
部分代码