How to reload a page from another php file
I am trying to reload a page (index.php) from another php file (script.php) if the condition is met. I am using script.php using jquery (not sure if it is jquery but see below code) from global.js
This is how they start ...
global.js runs on button click from login.php
$('input#btnlogIn').on('click', function() {
var pword1 = $('input#user_pword').val();
var name1 = $('input#user_name').val();
if(($.trim(name1) !=='') &&($.trim(pword1) !=='' )){
$.post('ajax/login_script.php', {user_name: name1, user_pword1: pword1}, function(data) {
$('div#log_inerrormsg').text(data);
});
}
});
This is the code I am trying to execute -> login_script.php
if(count($results) > 0 && password_verify($pssword, $results['password'])){
$loggeduser = $results['username'];
$_SESSION["uid"]=$loggeduser;
//code to refresh index.php or a specific div inside index.php
}
Below $_session["uid"]=$loggeduser;
I need some code, be it javascript or whatever, that can help me reload index.php or part of a div inside index.php. I am trying to change the title menu if a member is registered.
source to share
you need to use in common function file
function user_logged_in(){
return (isset($_SESSION['uid'])) ? TRUE : FALSE;
}
and in global.js in post () callback method redirect user .. use
window.location.href = 'index.php' // the link you want user to go to after log in
and in your index.php code use
if(user_logged_in() === true){
// code if user already logged in
}else{
// user not logged in
}
if you don't want to use the function only in index.php use
if(isset($_SESSION['uid'])){
// code if user already logged in
}else{
// user not logged in
}
for a more detailed explanation, you cannot redirect the user from a php file which is used by $ .post () or $ .ajax () .. you are redirecting the js callback function
source to share
You can write something like "OK" in login_script.php and check jQuery:
if(data == 'OK'){
location.reload();
} else{
//write error msg
}
Or specify what messages you want to receive from login_script.php.
More on location.reload()
JQuery: Refresh / Reload Page on Button Click
source to share