Mr Sorbose
Mr Sorbose

Reputation: 777

posting multidimensional array from checkbox

I have seen a couple of questions regarding posting arrays from checkboxes, however I havent seen anything attempting to do what I want to do.

I have a list of checkboxes that submit data populated in foreach loop of data from a database.

<input type="checkbox" name="phonelist[]" value="<?=strtoupper($device['id']);?>"/>

This is how I am currently returning multiple items for the checkbox phone list. Howver is it possible to add another value in the same value section of the checkbox but under a different item in a multidimensional array? e.g

<input type="checkbox" name="phonelist[][]" value="<?=strtoupper($device['id']);?><?=strtoupper($device['another value']);?>"/>

Im aware my "Psuedocode" is wrong but I hope it gets across the idea I wish to achieve.

Upvotes: 2

Views: 7578

Answers (2)

Giona
Giona

Reputation: 21114

I think you better approach it this way:

<input type="checkbox" name="phonelist[<?=strtoupper($device['id']);?>]" value="<?=strtoupper($device['id']);?>"/>

then

<input type="checkbox" name="phonelist[<?=strtoupper($device['id']);?>][<?=strtoupper($device['another value']);?>]" value="<?=strtoupper($device['id']).strtoupper($device['another value']);?>"/>

So you can still foreach $_POST['phonelist'] but keep a reference value.

Upvotes: 3

Mihai Iorga
Mihai Iorga

Reputation: 39704

Yes, add id for each section:

<input type="checkbox" name="phonelist[0][]" value="<?=strtoupper($device['id']);?><?=strtoupper($device['another value']);?>"/>
<input type="checkbox" name="phonelist[0][]" value="<?=strtoupper($device['id']);?><?=strtoupper($device['another value']);?>"/>
<input type="checkbox" name="phonelist[1][]" value="<?=strtoupper($device['id']);?><?=strtoupper($device['another value']);?>"/>
<input type="checkbox" name="phonelist[1][]" value="<?=strtoupper($device['id']);?><?=strtoupper($device['another value']);?>"/>

Upvotes: 8

Related Questions