I have a bootstrap php login modal with username and password..
There is a check when I submit my form for several thing: Do the email and password match? Does the email exist in the database etc.
When one of the checks fails the user will not login and an error will be stored in the php: $error = 1;.
The problem is that the modal closes when the form submits, which is fine when the login is good. But when the login fails and a error appears. Than the modal should not be close and the error must be shown in the modal.
Login code:
<?php
include('connection.php');
if (isset($_POST['proceed'])) {
$username = $_POST['login'];
$password = $_POST['password'];
/***** GUIDANCE *****/
$guidance = "SELECT * FROM faculty WHERE Faculty_ID = ? AND Password = ? AND Position='Guidance' ";
$stmt = $conn->prepare($guidance);
$stmt->bind_param('is', $username, $password);
$stmt->execute();
$result = $stmt->get_result();
/***** TEACHER *****/
$teacher = "SELECT * FROM faculty WHERE Faculty_ID = ? AND Password = ? AND Position='Teacher' ";
$stmt = $conn->prepare($teacher);
$stmt->bind_param('is', $username, $password);
$stmt->execute();
$result1 = $stmt->get_result();
/***** STUDENTS *****/
if ($result -> num_rows > 0) {
$row = $result -> fetch_object();
if($row->Status!='Active'){
$inactive = 1;
}else{
$_SESSION['access'] = 1;
$_SESSION['login'] = $row -> Faculty_ID;
$_SESSION['Faculty_Firstname'] = $row -> Faculty_Firstname;
header('Location: guidance/index.php');
}
}elseif ($result1 -> num_rows > 0) {
$row = $result1 -> fetch_object();
if($row->Status!='Active'){
$inactive = 1;
}else{
$_SESSION['access'] = 2;
$_SESSION['login'] = $row -> Faculty_ID;
$_SESSION['Faculty_Firstname'] = $row -> Faculty_Firstname;
header('Location: pages/teacher/index.php');
}
} elseif ($result2 -> num_rows > 0) {
$row = $result2 -> fetch_object();
$_SESSION['access'] = 3;
$_SESSION['Student_ID'] = $row -> Student_ID;
//$_SESSION[] = $row ->
header('Location:student/index.php ');
}else{
$error = 1;
}
}
?>
Boostrap modal code:
<div class="modal fade modal-open" role="dialog" id="loginModal1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Account Login</h3>
<button type="button" class="close" data-dismiss="modal">×</button>
<div style="padding-top:30px" class="panel-body" >
<div class="alert alert-danger <?php if(!isset($error)) echo 'hide';?>" style="margin-top: -10px">
Invalid username/password! Email administrator in link below to recover it.
</div>
<div class="alert alert-warning <?php if(!isset($inactive)) echo 'hide';?>" style="margin-top: -10px">
This account is no longer valid. Thank you!
</div>
<div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>
</div>
<div class="modal-body">
<form method="post">
<div class="form-group">
<label for="email">ID:</label>
<input type="text" class="form-control" required name="login">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" required name="password">
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-warning text-white " name="proceed">Signin</button>
</div>
</form>
</div>
</div>
</div>
</div>