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!
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.