Reputation: 33
I am trying to reload a page (index.php) from another php file (script.php) if a condition is met. I am running script.php using jquery (not sure if this is jquery but please see below code) from global.js
Here is how they run...
global.js runs when a button from login.php is clicked
$('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 workout -> 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 a code, whether a javascript or anything, that can help me reload index.php or a portion of a div inside index.php. I am trying to change the header menu if a member logged in.
Upvotes: 3
Views: 1668
Reputation: 24001
you need to use in general function file
function user_logged_in(){
return (isset($_SESSION['uid'])) ? TRUE : FALSE;
}
and in global.js in post() method callback 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 dont want to use a function just in index.php use
if(isset($_SESSION['uid'])){
// code if user already logged in
}else{
// user not logged in
}
for more explaination you can't redirect user from php file which used by $.post() or $.ajax() .. you redirect in js callback function
Upvotes: 1
Reputation: 19
You can write in login_script.php something like "OK" and check in jQuery:
if(data == 'OK'){
location.reload();
} else{
//write error msg
}
Or specify which messages you suppose to get from login_script.php.
More about location.reload()
Jquery : Refresh/Reload the page on clicking a button
Upvotes: 1