I have an issue with php form validation, it works fine but it does not work as expected, right now I have two types of php form validation in use, one is to check if four fields have data in them and the other is to check if the right format is used for the E-Mail field.
The problem I have is that if I don't do any of the fields and click submit, I expect the "do these four fields" error message to pop up, but instead the E-mail validation does, if I do the e-mail fine then the required field message pops which is not what I want.
Code, I am aware that for the validate email, the if statement has no code in it, I don't really know what to put there
//Check if these fields have data in them
if(isset($_POST['email']) || isset($_POST['password']) || isset($_POST['confirm']) || isset($_POST['username']) ){
if(!$_POST['email'] || !$_POST['password'] || !$_POST['confirm'] || !$_POST['username'] ){
$error = "Error. Please write in all required fields with the * symbol";
}
// Validate email format
//https://www.w3schools.com/php/php_form_url_email.asp
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
} else {
$error = "Error. Please write in the correct E-Mail format";
}
EDIT just want to clarify, I tried moving the validate email code above the checking code but it keeps a constant error message when the user enters the form to write in all the required fields
Entire PHP
<?php
//Check if these fields have data in them
if(isset($_POST['email']) || isset($_POST['password']) || isset($_POST['confirm']) || isset($_POST['username']) ){
if(!$_POST['email'] || !$_POST['password'] || !$_POST['confirm'] || !$_POST['username'] ){
$error = "Error. Please write in all required fields with the * symbol";
}
// Validate email format
//https://www.w3schools.com/php/php_form_url_email.asp
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
} else {
$error = "Error. Please write in the correct E-Mail format";
}
if(!$error){
//No errors - let’s create the account
//Encrypt the password with a salt
$encryptedPass = password_hash($_POST['password'], PASSWORD_DEFAULT);
//Insert DB
$query = "INSERT INTO users (user_email, user_password, user_forename, user_lastname, user_name, user_gender, user_country, user_number) VALUES (:email, :password, :firstname, :lastname, :username, :gender, :country, :mobile)";
$result = $DBH->prepare($query);
$result->bindParam(':username', $_POST['username']);
$result->bindParam(':firstname', $_POST['firstname']);
$result->bindParam(':lastname', $_POST['lastname']);
$result->bindParam(':email', $_POST['email']);
$result->bindParam(':gender', $_POST['gender']);
$result->bindParam(':country', $_POST['country']);
$result->bindParam(':mobile', $_POST['mobile']);
$result->bindParam(':password', $encryptedPass);
if($result->execute()){
echo '<div class="alert alert-success" role="alert">Registration Successful!</div>';
echo "<script> window.location.assign('index.php?p=login'); </script>";
}
}
HTML
<div class="row">
<div class="main-login main-center">
<h1>Register</h1>
<form action="index.php?p=register" method="post">
<?php if($error){
echo '<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
'.$error.'
</div>';
} ?>
<p>
* Required Fields
</p>
<div class="form-group">
<label for="email" class="cols-sm-2 control-label">* Username</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="username" id="username" placeholder="Username"/>
</div>
</div>
</div>
<div class="form-group">
<label for="email" class="cols-sm-2 control-label">* E-Mail Address</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="email" id="email" placeholder="example@hotmail.com"/>
</div>
</div>
</div>
<div class="form-group">
<label for="password" class="cols-sm-2 control-label">* Password</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
<input type="password" class="form-control" name="password" id="password" placeholder="Password"/>
</div>
</div>
</div>
<div class="form-group">
<label for="confirm" class="cols-sm-2 control-label">* Confirm Password</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
<input type="password" class="form-control" name="confirm" id="confirm" placeholder="Confirm Password"/>
</div>
</div>
</div>
<div class="form-group">
<label for="email" class="cols-sm-2 control-label">First Name</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user-circle fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="firstname" id="firstname" placeholder="First Name"/>
</div>
</div>
</div>
<div class="form-group">
<label for="email" class="cols-sm-2 control-label">Last Name</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user-circle fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="lastname" id="lastname" placeholder="Last Name"/>
</div>
</div>
</div>
<div class="form-group">
Gender:
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-outline-info active">
<!-- This button is checked by default for the user -->
<input type="radio" name="gender" id="gender_male" value="Male" autocomplete="off" checked=""> Male
</label>
<label class="btn btn-outline-info">
<input type="radio" name="gender" id="gender_female" value="Female" autocomplete="off"> Female
</label>
</div>
</div>
<div class="form-group">
<label for="address" class="cols-sm-2 control-label">Country</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-address-card" aria-hidden="true"></i></span>
<input type="text" class="form-control" id="placesSearch" placeholder="Country" name="country">
</div>
</div>
</div>
<div class="form-group">
<label for="mobile" class="cols-sm-2 control-label">Mobile Number</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-phone fa-lg" aria-hidden="true"></i></span>
<input type="mobile" class="form-control" name="mobile" id="mobile" placeholder="Mobile Number"/>
</div>
</div>
</div>
<div align="center" class="checkbox">
<label><input type="checkbox" value=""> Sign up for BakeryUp Newsletters!</label>
</div>
<div class="form-group ">
<button type="submit" class="btn btn-success btn-lg btn-block login-button">Join</button>
</div>
<div align="center">
By clicking 'Join' you are agreeing to our Terms and Conditions, Cookie Policy and Privacy Policy.
</div>
</form>
</div>
</div>