如何检查是否有字段的形式是空的php


How to check if any fields in a form are empty in php

每当我在表单中提交一些东西时,我想检查是否有任何字段是空的。到目前为止,我没有工作

$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

您的表单缺少方法…

<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.";
}
Specify POST method in form
<form name="registrationform" action="register.php" method="post">

your form code
</form>