Muhammad Harits Syaifulloh

Thursday, August 25, 2016

Using unchecked and checked boxes in array

Case :

I have a table that allows the user to add rows to it. The table has four columns. Items, Qty, Price, and Tax. The tax column is a check box. I am using the hidden input to set a value for an unchecked box. This way I can take the post data and know if I should charge tax on a specific row. (0 value means no tax, 1 value means charge tax) The problem I have is when putting check boxes in an array it will always pick up the hidden value.

Table :

For example: I have four rows. The first and last row are checked. The array looks like this [0,1,0,0,0,1] I need the array to look like this [1,0,0,1] Is this possible or do I need to try a completely different method of getting unchecked and checked boxes? I have tried to come up with a loop that will take away zeros but I can always find a combination of checked and unchecked.

Solution :

Unchecked checkboxes are simply never sent to server, and you need to adjust your form to accommodate that. I would suggest something like:

And then you can reliably test for whether or not that a given box was checked by checking for the existence of the tax index in the resulting array which should look something like:

No comments:

Post a Comment