Skip to content
Advertisement

If select option is unchecked, delete it from Database – Bootstrap Multiselect

I have an html table with bootstrap-multiselect in one of the columns. I want the database to delete the role when the checkbox is unchecked.

I am listening to the changes using if (isset($_POST['roles_checkbox'])). Therefore, when the checkbox is unchecked, it never gets called and nothing happens. If the user has 2 roles and 1 gets unchecked it gets deleted. Nevertheless, if the user has 1 role it cannot be deleted. I want the users to be able to have no role assigned to them.

JavaScript

Advertisement

Answer

I think you’re on the right path with your attempt at deleting all current user roles before reinserting the new ones. However, I think this is also where the issue lies.

Try instead of this:

JavaScript

Doing this:

JavaScript

EDIT based on comments

Knowing that user can have no roles, you would start be removing your initial input check if ($_POST['roles_checkbox']) and reqlacing it for a user id check i.e. if ($_POST['user_id']) this allows you to continue running the rest of your process without relying on roles input. See my updated example below…

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