Jump to content

[PHP/Javascript] Best way to display errors, warnings, others

Marxtai

Hello everyone!

 

I'm getting pretty good at working with PHP and Javascript (& jQuery) to make good login and registration system, using AJAX and that sort of thing. However, I struggle with one thing : I don't know how to manage the various alerts. For example, when you login successfully, I'd like to display a little message saying something like "You have been logged in successfully", using a Boostrap 'alert-success' for example. The problem is that I don't know what's the most convenient way of doing that.

 

At the moment I was showing it above the form once you've entered the good credentials, and it would say "You are being redirected" with a redirect a few seconds after that. However, that's not very optimal, and if they have JS disabled or blocked they will stay on this page forever - until they manually change page - which isn't convenient.

 

I thought of using sessions variables to handle that, but I really don't know what's the best way to do that. 

 

I could show some code if needed but at the moment it's pretty basic (just a login form with the basic checks), I need guidances on figuring out how to do it. After that, I can start the coding part. :P

 

So, what do you guys recommand for a user-friendly way to display various alerts, error messages or else? It would be a bonus if it was reusable for other things later on (ex: broadcast messages), but that's not the main objective.

 

Thank you all for your help!

Marxtai

Link to comment
Share on other sites

Link to post
Share on other sites

I dont actually know any Javascript or Php but I understand what you are talking about, so I can help with the concept but not the code :D

 

I would use dropdown alerts or cookie handled popups. 

 

The first would just be such as a notification bar when you receive a message while using your phone. The message is displayed for X time or the user needs to close it manualy. This would happen in a topmost layer of the page. This is a simple, minimalist way of handling the notification system. 

The Popup would work in a different manner. the interface would be just a *rectangle with an x button and the text* on a transparency which would be placed in the desired position of the page, a corner or in the middle. This would pop up on each and every page load until the user closes the dialog. thats what i meant with cookie managed. the variables could be in cookies or whatever you use in web programming (i only know how to program for iOS and mac OS lol) and these would be stored and deleted when necesary.

 

I know the first way is viable, the second is quite more complex. Just remember that they need to be as user-friendly as possible and not intrude in the use experience. 

Hope it was helpfull. If you need anything more just ask and ill answer if im around.

GL

Planning on trying StarCitizen (Highly recommended)? STAR-NR5P-CJFR is my referal link 

Link to comment
Share on other sites

Link to post
Share on other sites

I think the best way to show an alert like that is to do it after the first successfully login.  So in what ever session handler you have if the person is logging in you can show the alert at the top of the page in some kind of green box that disappears in a second, or two and goes away.  Then you just set a flag in your cookie file for a Boolean.  When the person returns if they aren't logged in any more obviously the cookie is destroyed and you can show a little red box at the top of the screen that says you aren't logged in any more and you could provide a little link that say login.

Link to comment
Share on other sites

Link to post
Share on other sites

Modern frameworks use 'flash' data to store information that should only persist through a single request.

 

This is done using session data, as you have already thought of.

 

A simple way of accomplishing this would be to create methods to set and get flash data. For example, you could define a set method with an (associative) array parameter and push however many messages you need into it, and store it in a session variable.

 

When a user successfully logs on, or you need to display a message on the next page, you could set a flash alert like so:

if ($login->success()){    flash['login_success'] = 'You have been successfully logged in!';}

You could then use the get method to retrieve and iterate over the array and display all messages on the new page. To make sure the data only persists for one request, you would empty the session variable in the get method after retrieving the data.

 

If you can understand it, I'd take a look at Rails' implementation of the 'flash' over at GitHub - https://github.com/rails/rails/blob/9fc7a6fcedd3adc820d9d481d9362313c356747b/actionpack/lib/action_dispatch/middleware/flash.rb

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×