Skip to content
Advertisement

How do i pass multiple Variables to a modal

So I have succeeded in passing the variables from one modal to another.The problem is they are multiple variables and I am looping them with the PHP foreach function, all the items are displayed but when I click on any item only the first variable is passed and not the specific one I chose. How do I fix this here is the code for the initial modal

JavaScript

this is the JS Code where i pass the values to the other modal

JavaScript

And this is the code of the modal where i want the data to be passed

JavaScript

Advertisement

Answer

You’re currently using a foreach loop to assign the same ID to multiple html tags. For simplicity of this answer, I will be taking your <img> tag with the ID cardimage as example. Furthermore I’d like to note that your ID’s should always be unique!

The problem

JavaScript

Every team you’re looping through the $category_products array, you’re adding another one of those tags, identical to the last one.

For example if you loop 3 times through the $category_products array, you will get 3 times the same ID.

JavaScript

When there are multiple identical ID’s on a webpage, your javascript will pick the first one it can find, and run your script on that, as it only expects one result. To fix this, you should make all your ID’s unique.

A possible solution

First of all, make all of your ID attributes unique. In your loop you’re looping through $category_products as $cp, and you appearantly have the variable $cp['inventory_id']. I’ll be taking that as unique id for this answer for now.

You could append this unique ID behind your ID like this:

JavaScript

Then change your addModalDetails() like this:

JavaScript

and call it like this:

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