Reputation: 17
I made a user registration form and am now working on the error handling but I can't seem to figure out what is going wrong.
So far I've made one error handler to check if password and confirm password upon registration do match, if not I need the error to display in the form. I would love to have the password match statement to be displayed in or under the form if the passwords in the registration form do not match, i would also love to add messages later to confirm if user exists or not or if fields are left empty.
My signup script:
if (isset($_POST['register'])) {
$name = filter_input(INPUT_POST, 'name');
$email = filter_input(INPUT_POST, 'email');
$password = filter_input(INPUT_POST, 'password');
$cPassword = filter_input(INPUT_POST, 'cPassword');
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
if ($password != $cPassword)
$msg = "Wachtwoord komt niet overeen!";
else {
$hash = password_hash($password, PASSWORD_BCRYPT);
$sql = "INSERT INTO users (name, email, password) VALUES (?,?,?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$name, $email, $hash]);
$msg = "U bent geregistreerd!";
header( "Refresh:3; url=/Login2/inloggen.php", true, 303);
}
}
My form contains the following code and had the above file included:
<?php
require'includes/layouts/header.php';
require'includes/signup.inc.php';
?>
<div class="login-page">
<div class="form">
<?php foreach ($errors as $error): ?>
<p><?= $error ?></p>
<?php endforeach ?>
<form class="login-form" action="registreren.php" method="post">
<input type="text" placeholder="Username" name="name"/>
<input type="email" placeholder="E-mail address" name="email"/>
<input type="password" placeholder="Password" name="password"/>
<input type="password" placeholder="Password" name="cPassword"/>
<button name="register" type="submit">create</button>
<p class="message">Bent u al geregistreerd? <a
href="index.php">Keer terug</a></p>
</form>
</div>
</div>
<?php
require'includes/layouts/footer.php';
?>
Upvotes: 0
Views: 74
Reputation: 61925
In the signup script you create a (string) variable called $msg
and then never use it. In the form you refer to a (array) variable called $errors
which doesn't appear to be defined anywhere. The two things don't match up (either by name or by type), which is the obvious root of your issue.
Instead of
if ($password != $cPassword)
$msg = "Wachtwoord komt niet overeen!";
try
$errors = array();
if ($password != $cPassword)
$errors[] = "Wachtwoord komt niet overeen!";
P.S. You also have a syntax error as per the comments - make sure you write <?php endforeach; ?>
- almost all lines of code in PHP must end with a semi-colon.
Upvotes: 2