When I clicked on register button leaving an empty field then it should be displaying an error message that username or email is required. Actually when I click on register then page redirects to index.php and I have to click to button to see a form and after that I can see the error on that form. I dont want this. I want to stay on register page if user click on register upon leaving any empty field and also display the error mesaage.
This is html code
<form action="<?= ROOT_URL ?>signup-logic.php" method="POST">
<div class="popup-form" id="my-form">
<div class="form-box">
<i id="my-close-btn" class="uil uil-multiply"></i>
<div class="button-box">
<div id="btn"></div>
<button id="log-btn" class="toggle-btn" type="button">Log In</button>
<button id="reg-btn" class="toggle-btn" type="button">Register</button>
</div>
<div class="social-icons">
<a href="#"><i class="fa-brands fa-twitter"></i></a>
<a href="#"><i class="fa-brands fa-facebook"></i></a>
<a href="#"><i class="fa-brands fa-google"></i></a>
</div>
<div id="login" class="form-group">
<input type="text" class="input-field" placeholder="Username">
<input type="text" class="input-field" placeholder="Password">
<input type="checkbox" id="rem-chk" class="check-box"><span class="rem">Remember Me</span>
<button type="submit" class="submit-btn">Log In</button>
<p id="r_msg">Registration Successful. Please log In</p>
</div>
<div id="register" class="form-group">
<?php if (isset($_SESSION['signup'])) : ?>
<div class="err_msg">
<p id="g_msg">
<?=
$_SESSION['signup'];
unset($_SESSION['signup']);
?>
</p>
</div>
<?php endif ?>
<input type="text" name="email" value="<?= $email ?>" class="input-field" placeholder="Email">
<input type="text" name="username" value="<?= $username ?>" class="input-field" placeholder="Username">
<input type="text" name="createpassword" value="<?= $createpassword ?>" class="input-field" placeholder="Password">
<input type="text" name="confirmpassword" value="<?= $confirmpassword ?>" class="input-field" placeholder="Confirm Password">
<div class="type-chk">
<input type="checkbox" class="check-box">
<span class="i-agree">I agree to terms & conditions</span>
</div>
<button type="submit" name="submit" class="submit-btn">Register</button>
</div>
</div>
</div>
<!-- <a href="" class="pro-btn" id="my-form-btn">Visit Us To Promote Your Website</a> -->
<button class="pro-btn" type="button" id="my-form-btn">Click To Promote Your Website</button>
</form>
This is php script file
require 'config/database.php';
if(isset($_POST['submit'])) {
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$username = filter_var($_POST['username'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$createpassword = filter_var($_POST['createpassword'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$confirmpassword = filter_var($_POST['confirmpassword'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
// echo $email;
if(!$email) {
$_SESSION['signup'] = 'Please enter a valid email';
} else if (!$username) {
$_SESSION['signup'] = 'Please enter a username';
} else if (strlen($createpassword) < 8 || strlen($confirmpassword) < 8) {
$_SESSION['signup'] = 'Password should be 8+ character';
} else {
if($createpassword !== $confirmpassword) {
$_SESSION['signup'] = 'Password do not match';
} else {
$hashed = password_hash($createpassword, PASSWORD_DEFAULT);
$user_chk_query = "SELECT * FROM users WHERE username = '$username' OR email = '$email'";
$user_chk_result = mysqli_query($conn, $user_chk_query);
if(mysqli_num_rows($user_chk_result) > 0) {
$_SESSION['signup'] = "username or email already exists";
echo "username or email already exists";
}
}
}
if(isset($_SESSION['signup'])) {
//pass form data back to page
$_SESSION['signup-data'] = $_POST;
header('location: '. ROOT_URL . 'index.php');
die();
} else {
//otherwise insert user into database
$insert_query = "INSERT INTO users SET email = '$email', username = '$username', password = '$hashed'";
$insert_user = mysqli_query($conn,$insert_query);
if(!mysqli_error($conn)) {
//redirect to login page
$_SESSION['signup-success'] = 'Registration successful. Please log in';
header('location: '. ROOT_URL . 'index.php');
die();
}
}
} else {
header('location: '. ROOT_URL . 'index.php');
die();
}```