简体   繁体   中英

Form submits even when all fields aren't filled out

I'm new to php and I don't quite understand why this form is submitting under all circumstances... So my question is:

How do I fix this so the form only submits when the user fills out all 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 both && and || operators however it always submits no matter what field you filled out.

<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 this seems like an awesome community to be involved in!

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.

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');
    }

Because you are using && (and) which means all of the conditions must be true. Use || (or) and this will work.

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 ||... !A || !B || !C ||... !A || !B || !C ||... or !(A && B && C && ...) .

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.

尝试

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM