10

每当我在表单中提交内容时,我都想检查是否有任何字段为空。到目前为止,我没有工作

$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

4

2 回答 2

16

您的表单缺少方法...

<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.";
}
于 2013-01-15T05:13:43.440 回答
2
Specify POST method in form
<form name="registrationform" action="register.php" method="post">


your form code

</form>
于 2013-01-15T05:16:45.170 回答