检查表单是否已填写并发送消息

时间:2013-10-05 05:37:36

标签: php web

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

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

2 个答案:

答案 0 :(得分:0)

尝试使用empty函数

更改isset函数

答案 1 :(得分:0)

问题应该是你错过了php tag

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

试试这可能有效

由于

相关问题