Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, everybody. I have a login form which is, after submitting, being validated by PHP(checking the database etc.). Now, I need to send feedback to user (login fail or succes) by using Javascript. What is the right way of doing this?

What I have tried:

So far, I know I can do this by simply echoing the script that calles javascript function (<script> jsFuntion(message) ;</script>) or by using AJAX. But I'm asking myself what is the safest and the most correct way?
Posted
Updated 30-Jun-16 2:34am

1 solution

I think, the best way to solve this problem is to use 'SESSION'. Store the result (whether success or failure) in the session as sort of a flash message.

1. Set a session message after your operation in the process page.
2. Then in the redirected page check whether the session message is set or not.
3. If it is set then simply echo that message.

The below code may help you.

$_SESSION['message'] = '';

// In the process page, where you are checking for the authentication
if (logged_in) { // your logic for authenticating user
    $_SESSION['message'] = 'Success';
} else {
    $_SESSION['message'] = 'Failure';
}

// In the same Login page (if it fails) or in the Dashboard page (if it success)
if(isset($_SESSION['message'])){
    echo $_SESSION['message'];
}
?>


I hope this will help you.
 
Share this answer
 
Comments
PeMaCN 30-Jun-16 8:44am    
Is this also good solution if I have separate php file and if user stays on the same page after logged in?
Prava-MFS 30-Jun-16 8:47am    
It depends on what you need!! If you wanna stay on the same page after logged in, then just a normal page refresh (same page) would work with updating cookie/session data or with updating the menus.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900