I'm fairly new to PHP and I'm trying to make a login system. I am trying to output the error or success message to display in the same page as my form. I tried 'echo' for testing and it works, however it shows the message in a new tab. So I tried to turn the messages in to one variable, so in every case the variable gets updated with the new message. Then on the register.php page, I put in an PHP code where I would check if the message is not empty, and then echo out the variable. The problem is that I get redirected to the register.php page, only nothing shows up. Here is the code of my two files, I should mention that the registration process does work, it checks for the email and username if there are no duplicates, however it does not output any message.
register.php:
<?php
include 'header.php';
include 'nav.php';
include 'handlers/registerhandler.php';
require 'handlers/dbhandler.php';
?>
<section>
<div class="wrapper">
<p>If you already have an account, click <a href="index.php">here</a> to login.</p>
</div>
<div class="wrapper">
<?php
if (!empty($msg)) {
echo $msg;
}
?>
<form action="register.php" method="POST">
<ul>
<li><input type="text" name="firstname" placeholder="Enter your first name ..." required></li>
<li><input type="text" name="lastname" placeholder="Enter your last name ..." required></li>
<li><input type="email" name="email" placeholder="Enter your email adress ..." required></li>
<li><input type="text" name="username" placeholder="Enter your username ..." required></li>
<li><input type="password" name="password" placeholder="Enter your password ..." required></li>
<li><input type="password" name="confirmpassword" placeholder="Confirm your password ..." required></li>
<li><button type="submit" name="submit" value=1>Register</button></li>
</ul>
</form>
</div>
</section>
<?php
include 'footer.php';
?>
And the registerhandler.php:
<?php
session_start();
require 'dbhandler.php';
if (isset($_POST['submit'])) {
$msg = '';
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
$usernameExcistCheck = "SELECT * FROM user WHERE username='$username'";
$usernameCheckResult = mysqli_query($connect, $usernameExcistCheck);
$emailExcistCheck = "SELECT * FROM user WHERE email='$email'";
$emailCheckResult = mysqli_query($connect, $emailExcistCheck);
while ($usernameRow = mysqli_fetch_assoc($usernameCheckResult)) {
$db_username = $usernameRow["username"];
}
while ($emailRow = mysqli_fetch_assoc($emailCheckResult)) {
$db_email = $emailRow["email"];
}
if ($db_username == $username) {
$msg = "Username already excists.";
} else {
if ($db_email == $email) {
$msg = "E-mail is already registered";
} else {
if($password != $confirmpassword) {
die("Passwords do not match! Please try again.");
} else {
$confirmcode = rand();
$sql = "INSERT INTO user (firstname, lastname, email, username, password, confirmed, confirmcode)
VALUES ('$firstname', '$lastname', '$email', '$username', '$password', '0', '$confirmcode')";
$result = mysqli_query($connect, $sql);
if ($result) {
$to = $email;
$subject = "Activate your DuChambre.nl account!";
$message = "<html>
<head>
<title>Activate your DuChambre.nl account</title>
</head>
<body>
<p>Dear $firstname $lastname,<br><br>Your account needs to be activated in order to use it. Please click <a href='http://www.duchambre.nl/duchambre_loginsystem/verifyemail.php?code=$confirmcode&email=$email'>here</a> to activate your account. You could also copy and paste the link below.<br><br>http://www.duchambre.nl/duchambre_loginsystem/verifyemail.php?code=$confirmcode&email=$email<br><br>Please do not reply to this email. Enjoy your stay!<br><br>Cheers,<br>Bas
</p>
</body>
</html>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" ."\r\n";
$headers .= 'From: <activation@duchambre.nl>' . "\r\n";
$mail = mail($to, $subject, $message, $headers);
if ($mail) {
$msg = "Your account has been created, check your email to activate your account.";
//header("Location: register.php");
} else {
$msg = "Something went wrong, please try again.";
}
}
}
}
}
}
?>
I think I'm missing something, but I've spend 3 hours now trying to fix this. Thanks in advance for the help.