How to send only the quantity of products that were selected in the checkbox?

Asked

Viewed 55 times

0

I displayed a table of items and in this table I added a checkbox for each item and a field to pick the quantities of each item.

I’m managing to send to PHP only the items marked in the checkbox (that’s what I wanted).

But I can not do the same with quantity, because I am sending via POST the amount of all items in the table, both the items that were selected and those that were not.

I want to send only the amount of selected items, or who knows how to continue sending as well as this and then tidy the array there in PHP eliminating empty amounts.

How could I fix this? inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • could create a js function to add in a hiddden variable the id and value

  • Where is the code??

  • Quickly removes null values $arrayLimpo = array_filter($_POST['cantidad']);

1 answer

0

Instead of using the foreach, you would use a for with a counter $i that would be increased by 1. This $i would go to the value of your checkbox. So you would have its position in your table.

Add an Hidden input for each row in the table that would store the information $item['id'].

Thus, you would have the indexes that were populated in the $Cantidad vector, managing to recover the quantity of products and in the Hidden input the product id.

I don’t know if that’s the best solution, but that’s what came to my mind at the time.

Browser other questions tagged

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