php multiple checkboxes save in one field.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

php multiple checkboxes save in one field.

Post by cjkeane »

hi everyone.

i have several check boxes which i need to save as an array into one field in a database.
i can save each check box with a separate name and save them in separate fields in the database, but i'd really like them saved as an array and then i only have to check the array to see if one check box is checked.

i'd appreciate any helpful hints people could provide.

this is my check box code.

Code: Select all

<input type="checkbox" name="ACLRole[]"  value="User"<?php echo ($ACLRole == 'User' ? " CHECKED" : ""); ?>>User<br />
<input type="checkbox" name="ACLRole[]"  value="Manager"<?php echo ($ACLRole == 'Manager' ? " CHECKED" : ""); ?>>Manager<br />
<input type="checkbox" name="ACLRole[]"  value="Admin" <?php echo ($ACLRole == 'Admin' ? " CHECKED" : ""); ?>>Administrator <br />

User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php multiple checkboxes save in one field.

Post by Jonah Bron »

Why do you want it in an array? I don't understand the advantage of that over individual columns.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: php multiple checkboxes save in one field.

Post by cjkeane »

I wanted to validate or rather check the array to see if it had data, instead of validating separate check boxes.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: php multiple checkboxes save in one field.

Post by flying_circus »

Serialize your array and store it in the database as a string.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php multiple checkboxes save in one field.

Post by Jonah Bron »

I understand what you want to do, I just don't know why. The number of checkboxes is constant, right?
Post Reply