Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

forms

php

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

like image 524
Travis Nabbefeld Avatar asked Jan 15 '13 05:01

Travis Nabbefeld


2 Answers

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.";
}
like image 59
bipen Avatar answered Nov 01 '22 18:11

bipen


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


your form code

</form>
like image 21
Rajesh Vishwakarma Avatar answered Nov 01 '22 18:11

Rajesh Vishwakarma