I am creating a web app in which login can be done from any page. To achieve this I used a modal for login form. On successful login, I want the user to stay on the same page. This is my login verification script.
$.ajax({
dataType: "json",
type: "POST",
data: {
login: true,
email: cleanScripts($('#username').val()),
password: cleanScripts($('#password').val())
},
url: "plugins/validator.php",
success: function () {
location.reload();
},
error: function () {
$('.error').show();
}
});
On the header template (where I have the login button) I check whether user is logged in and update the html code as given below
if (!isset($_SESSION['userIdMain']))
echo '<li>
<a href="#" onclick="showLoginForm();">
<i class="dashicons dashicons-lock"></i>
<span>LOGIN/SIGN UP</span>
</a>
</li>';
else echo '<span>Hi, ' . $_SESSION["userName"] . '</span>';
Header template is included in the page using
<?php include 'templates/header.php' ?>
Ideally, on successful login, the page should refresh and instead of LOGIN/SIGNUP button, it should show the name of logged in user. But the UI is not updating. Even after successful login, the page shows LOGIN/SIGNUP button.
When I navigated to another pages, it is working as expected. The UI is not updating for the page from where login is performed. Any idea why this is happening?
Thanks