The form is submitted even if all fields are empty

I am new to php and I don’t quite understand why this form is submitted under any circumstances ... So my question is:

How can I fix this, so the form is only submitted when the user fills in all the fields?

if (!$_POST['username'] && !$_POST['password'] && !$_POST['repassword'] && !$_POST['user_firstname'] && !$_POST['user_lastname'] ){ header('Location: register.php?msg=You did not complete all of the required fields'); } 

I used the operators && and || However, it always represents, no matter what field you fill in.

 <form action="createuser.php" method="post" name="registration_form" id="registration_form"> <label>Email</label> <input name="username" type="text" id="username" size="50" maxlength="50" /><br /> <label>First Name</label> <input name="user_firstname" type="text" id="user_firstname" size="50" maxlength="50" /><br /> <label>Last Name</label> <input name="user_lastname" type="text" id="user_lastname" size="50" maxlength="50" /><br /> <label>Password</label> <input name="password" type="password" id="password" size="50" maxlength="100" /><br /> <label>Re-type Password</label> <input name="repassword" type="password" id="repassword" size="50" maxlength="100" /><br /> <input type="submit" value="Register" name="submit" /> 

Thanks in advance for any help, and it looks like a terrific community to be involved!

+4
source share
6 answers
 if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['repassword']) || empty($_POST['user_firstname']) || empty($_POST['user_lastname']) ){ header('Location: register.php?msg=You did not complete all of the required fields'); } 

EDIT: True, as follows from the comments, the check should be more substantial than this. redirecting after validation is not a good idea, try sending the form to yourself instead. If there are no errors, continuing to process the form data, if there are errors, they are set in the variable / object / array and display them in the form / re-fill the form with the message data.

+3
source

The operator ! (not) works best with booleans, but all of your values ​​are strings. Instead:

 !$_POST['username'] 

to try:

 !empty($_POST['username']) 

This is codi if you want to do something when all the fields are filled:

 if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['repassword']) && !empty($_POST['user_firstname']) && !empty($_POST['user_lastname']) ){ header('Location: register.php?msg=You completed all of the required fields'); } 

If you want to accomplish something when at least one field is empty, try:

 if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['repassword']) || empty($_POST['user_firstname']) || empty($_POST['user_lastname']) ){ header('Location: register.php?msg=You did not complete all of the required fields'); } 
+3
source

Because you use && (and), which means that all conditions must be true. Use || (or) and it will work.

0
source

You use && this is true only if all conditions are true, i.e. you only redirect when none is set to your values. If you want to return it if any of them are not installed, you can say !A || !B || !C ||... !A || !B || !C ||... !A || !B || !C ||... or !(A && B && C && ...) .

0
source

The form behaves exactly as it was intended to be performed.

To test your form you need to use JavaScript:

 <input type="button" onclick="val()" value="Register" name="submit" /> 

JS:

 function val() { ///your validation code. } 

There are many authentication scripts and plugins available in the wild.

-1
source

Try

 if(empty($_POST['item'])) && ... 
-1
source

Source: https://habr.com/ru/post/1439012/


All Articles