2

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.

2 Answers 2

8

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']);?>"/>
Sign up to request clarification or add additional context in comments.

1 Comment

Adding ids for first subscript should be sufficient. Starting from 0 might be a good idea too.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.