且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-18 16:43:42

您的表单缺少方法...

 < form name =registrationformaction =register。 phpmethod =post> // here 

anywyas检查发布的数据,您可以使用 isset() ..


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




  if(!isset($ firstname )|| trim($ firstname)=='')
{
echo你没有填写必填字段。
}


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

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()..

Determine if a variable is set and is not NULL

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