have been struggling so long with this...
Story: I tried to add "password confirm" ($repassword) into my registration form. Also added a string which says "Passwords doesn't match" which should activate if that is the case (if passwords aren't same).
Problem: Everything works fine, except it won't show that message if passwords aren't same.
A little more story if you're not in a hurry: I have tried several things. E.g putting that into function (function valid_pws) or putting it into a single if statement. Some of them works by outputting "Passwords doesn't match" text, BUT then it always shows that message if user has clicked "Register" button. Oh and there's everything fine with database. It always understands what I want to do, just can't that text to show (if it's needed) or hide (if it's not needed).
PHP code:
<?php
require ('insert.php');
function valid_pws($password,$repassword) {
// Validate pws.
if (empty($password || $repassword)) {
return false;
}
else if ($password !== $repassword) {
// error matching passwords
return false;
}
else {
return true; // passwords match
}
}
// create that user
function create_user($name, $username, $password, $email) {
require ('insert.php');
$query = "INSERT INTO `users` (Name, Username, Password, EMail) VALUES('$name','$username', '$password', '$email')";
$result = mysqli_query($connection, $query);
if($result){
return true; // Success
}else{
return false; // Error somewhere
}
}
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
$name = $_POST['name'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
// I'm not using hash atm
$hash = password_hash($password, PASSWORD_DEFAULT);
//valid_pws function in a var
$pwerr = valid_pws($password,$repassword);
//if there's everything fine with passwords then good
if ($pwerr === true){
//if there's everything fine with creating user then good
if (create_user($name, $username, $password, $email)) {
$smsg = "User registration success.";
}
else{
//if there's something wrong with pws, then error. Should get it from function if false is returned... or not?
if($pwerr === false){
$rmsg = "Passwords doesn't match";
}
else {
// if there's something else with registration, then error
$fmsg = "User registration failed.";
}
}
}
}
mysqli_close($connection);
?>
HTML code:
<form action="" method="POST" class="form-horizontal" role="form">
<!-- How registering went -->
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<?php if(isset($rmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $rmsg; ?> </div><?php } ?>
<!-- Registration form begins -->
<h2>Register</h2><br>
<label for="Name">Name</label>
<input name="name" type="text" id="name" maxlength="40" placeholder="Name" class="form-control" required autofocus> <!-- end -->
<label for="email">Email</label>
<input name="email" type="email" id="email" maxlength="65" placeholder="Email" class="form-control"> <!-- end -->
<label for="userName">User</label>
<input name="username" type="text" id="userName" maxlength="12" placeholder="Username" class="form-control" required> <!-- end -->
<label for="password">Password</label>
<input name="password" type="password" id="password" maxlength="50" placeholder="Password" class="form-control" required> <!-- end -->
<label for="password">Repassword</label>
<input name="repassword" type="password" maxlength="50" placeholder="Password again" class="form-control"> <!-- end -->
<button type="submit" class="btn btn-primary btn-block">Register</button>
</form>
You have messed conditions it should be: