Form Submits Even When All Fields Aren't Filled Out
Solution 1:
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 the comments suggest, the validation needs to be more substantial than this. redirecting after checking is not a good idea, instead try posting the form to itself. If there aren't any errors then continuing with processing the form data, if there are errors set them in a variable/object/array and display them on the form/repopulate the form with the post data.
Solution 2:
The !
(not) operator works best with booleans, but all your values are strings. Instead of:
!$_POST['username']
try:
!empty($_POST['username'])
This is the codi if you want to execute something when all fields are full:
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 execute 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');
}
Solution 3:
Because you are using &&
(and) which means all of the conditions must be true. Use ||
(or) and this will work.
Solution 4:
You're using && which is only true if all the conditions are true - that is to say that you do your redirect only in the case where none of your values are set. If you want to back it if any of them are unset, you'd say !A || !B || !C ||...
or !(A && B && C && ...)
.
Solution 5:
The form is behaving exactly as it was designed to do.
You need to use JavaScript to validate your form:
<input type="button" onclick="val()" value="Register" name="submit" />
JS:
function val() {
///your validation code.
}
There are many form validation scripts and plug-ins available out in the wild.
Post a Comment for "Form Submits Even When All Fields Aren't Filled Out"