0

如果用户未提交姓名、性别或宿舍,则在按下“注册”按钮时页面应显示红色错误。这是我的代码,但效果不佳。它总是给用户错误。

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

2 回答 2

0

问题应该是这个你错过了php tag

<?php if (isset($error)): ?>
                <div style="color:red"> You must fill out the form!</div>
                <?php endif ?>

试试这个可能有效

谢谢

于 2013-10-05T05:54:44.157 回答
0

尝试empty用函数改变isset函数

于 2013-10-05T05:46:11.867 回答