Skip to content
Advertisement

Setting General Messages Using Flash Message

I have a simple register form, my form validates but will not show error messages or validation messages

This is my form function

JavaScript

I call my validation function in my form page

JavaScript

so if there is an error it should set message but don’t.

now if it successfully it redirects to login and sets a flash message also and I call it with

JavaScript

That don’t display a message either

Flash message code

JavaScript

my goal is to use one set message for all notifications with styles but I cannot get none of the messages to display

Advertisement

Answer

I’ll assume you’re calling session_start() at the beginning of the script.

Your usage of functions makes the problem much easier to diagnose! Sometimes, though, it helps to have a different set of eyes look at it.

Your function set_message() has a couple of errors:

  1. The initialization of $_SESSION['flash_notifications'] should occur if it is empty, but instead you are initializing if it is not empty. Hence nothing can be added
  2. Malformed assignment. When you are building the message array to save in $_SESSION, there is no need to reassign $message. Also, usage of single quotes does not interpret variables within the quotes, so the html snippet is not what you expect.

Corrected function:

JavaScript

Note, it might be more understandable to write it this way:

JavaScript

Your function display_message() is almost correct as is, except you’re returning an array, not a string. If you’re going to print it, it must be converted into a string:

JavaScript

Then when you call it in your html, use the short print tag instead of the regular <?php tag:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement