Skip to content
Advertisement

Ajax / JQuery – Displaying flash message on success

I have a fully working flash system in PHP and am using it to send the user a success message once I create an entry in the DB.

On one of my forms I have a select field which I want the user to be able to seamlessly add entries too it without directing them away from a semi-completed form. The code I’m using is working well. The user clicks on ‘add a category’ (in the select label) it opens a modal, the user creates a new category, it updates the DB and the select field and closes the modal using AJAX. All working.

What I need to do is use or adapt my flash system to give the user a message to say all good your entry was added. I am very new to AJAX and on a steep learning curve!

This is my AJAX / JQUERY code: (I followed a tutorial to get here. The idea is to make this usable across the site when I need to add entries to a select, by adding ‘ajax’ to the form class.)

JavaScript

And this is the PHP setting the DB record (working) and how I normally trigger a flash message on page reload (messages also work):

JavaScript

And this is the flash code:

JavaScript

My PHP processing page, creates the entry in the DB and I set the flash message as normal. I think I don’t understand the interaction with how AJAX gets the returned success and setting a flash message.

Any thoughts?

Advertisement

Answer

Thanks to CBroe who pointed out the inherent problems with using a flash message mechanism I’ve added the following div at the bottom of the page and am now calling that direct with toast.show to give the message to the user.

I am not sure if that is the most affective way to do this but it works.

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