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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14331525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:05:08  来源:igfitidea点击:

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

phphtmlforms

提问by Travis Nabbefeld

whenever I submit something in my form i want to check if any of the fields are empty. So far what I have is not working

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

$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.";
}

and the form

和表格

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

and heres the registation page if it helps http://www.myjournal.tk/register.html

如果有帮助,这里是注册页面http://www.myjournal.tk/register.html

回答by bipen

your form is missing the method...

您的表单缺少方法...

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

anywyas to check the posted data u can use isset()..

无论如何要检查发布的数据,您可以使用isset()..

Determine if a variable is set and is not NULL

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

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

回答by Rajesh Vishwakarma

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


your form code

</form>