Mark checkboxes when choosing select value, coming from a PHP (Laravel) BD

Asked

Viewed 112 times

0

I have a problem I don’t know how to fix and I don’t know how to look. I’m building a system in the php - laravel. This system will have user levels.

I’ve done all the registration, changes, user views, groups and permissions. However, I stuck in a part that I probably have to use javascript, but I have very basic knowledge in JS.

When it comes to adding a user, I have a SELECT with the groups (e.g. ADMIN, USERS...) and a checkbox list with the general permissions ( of all groups)... until then I can select the group and what premises I want.

But here’s what I needed to do: That when I selected (e.g. ADMIN) in select, JS already filled in all the permissions that ADMIN is entitled to, and then the user could manually select whether to want more permission or not.

I need to do this because when adding the user, I need to know which permissions are right for each group, so know which one to register.

2 answers

0


I would recommend that you make an AJAX request to search for a JSON with standard permissions.

For example, you can create a route in Laravel ('/permissions/{group}') that you can check which group and return JSON, I don’t know how you saved it in BD, but at first just select and return that Laravel mounts JSON.

In your script, when the date comes with the answer of this AJAX, you navigate in a foreach this JSON, for each default entry, you search for the DOM using jQuery and arrow this checkbox as checked ().

An event in select for when the value changes, it fetches the default permissions from AJAX and is done.

If you want to do more hardcoded yet and ignore AJAX, you can put these JSON with the permissions of each group in an array in your own script, it will avoid requests to search for JSON but it is a less elegant solution from my point of view.

  • I was able to resolve using AJAX requests.

  • Thank you very much Evandro. Hugs

  • I’m sorry, I just saw that reply, is there anything else I can help you with? Or is everything working as you wanted it to?

  • Yes, I was not very familiar with php + js. Based on your comment you can go back and learn more about it. I saw that you can do a lot of cool things.

  • Good guy, good study!

0

First of all, thank you for your attention, Evander. Come on, come on... I never used Json, and as I said I know the basics of JS. But I really saw the logic of what you said, and I looked into those subjects.

See if I’m right: - I must create a JSON with the database settings: $group->permissions (array)(in PHP) - This via a route in the Laravel - Calling this JSON in the view via AJAX - Make a function in select to 'check' permissions when the group is selected. okay?

Now other doubts have arisen, if possible, I count on your help: 1st: when generating this JSON, do I need to generate a JSON file in some directory? or is it possible to generate a JSON variable and play for view, and be able to access it with JS? ---> vi the command, e.g. $grupojson = $group->permissions->to_json(); to transform my array(object) into JSON.

2nd I will always have to generate this JSON when creating a user, or I create once and leave it stored to access later?

After your answers we continue straight.

Thank you once again for your attention. I didn’t know much about this JSON, but from what I researched, I can do many useful things with it, especially the relationship with other languages.

Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.