如何检查表单中的任何字段是否为空

时间:2013-01-15 05:10:37

标签: php html forms

每当我在表单中提交某些内容时,我想检查是否有任何字段为空。到目前为止,我所拥有的不起作用

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$password = $_POST['password'];
$passwordconf = $_POST['passwordconf'];
$email = $_POST['email'];
$securityq = $_POST['securityq'];
$qanswer = $_POST['qanswer'];

if(empty($firstname) || empty($lastname) || empty($username) || empty($password) || empty($passwordconf) || empty($email) || empty($securityq) || empty($qanswer))
{
    echo "You did not fill out the required fields.";
}

和表格

<form name="registrationform" action="register.php">
    First Name:<input type="text" name="firstname">
    Last Name:<input type="text" name="lastname">
    Email:<input type="text" name="email">
    Username:<input type="text" name="username">
    Password:<input type="password" name="password">
    Confirm Password:<input type="password" name="passwordconf">
    Security Question:<input type="text" name="securityq">
    Answer:<input type="text" name="qanswer">
    <input type="submit" name="submit" value="Register">
</form>

如果有帮助http://www.myjournal.tk/register.html

,请注册注册页面

2 个答案:

答案 0 :(得分:13)

你的表单缺少方法......

<form name="registrationform" action="register.php" method="post"> //here

任何检查发布的数据你可以使用isset() ..

  

确定变量是否已设置且不是NULL

if(!isset($firstname) || trim($firstname) == '')
{
   echo "You did not fill out the required fields.";
}

答案 1 :(得分:2)

Specify POST method in form
<form name="registrationform" action="register.php" method="post">


your form code

</form>