Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
Thread: checkbox question
12-02-2012, 03:11 PM #1
- Join Date
- Aug 2012
- Thanked 0 Times in 0 Posts
I have a php form with a list of checkboxes (http://mandatorymarketinginc.com/web/sample-01.png)
These checkbox values were previously saved to a table called "comp-profile-03-specialties" and I got the code working on the form to to check and if specialty selected the checkbox is checked.
On the php form checkboxes name is "specialties".
However my problem comes in when I want to update the checkboxes to account for the possibilities if the a checkbox is de-selected or another checkbox is added.
How would I go about doing this?
12-02-2012, 06:37 PM #2
checkboxes name is "specialties
- Join Date
- Jun 2003
- Cottage Grove, Minnesota
- Thanked 1,158 Times in 1,149 Posts
I think we'll all do this a little different. I don't know if my way is the best method or not.
I usually give my list of checkboxes (named as an array like yours), the values:
.. name="specialties" value="1" ...
.. name="specialties" value="2" ...
.. name="specialties" value="3" ...
I know what those values refer to ... example, 2 is "Audio/Visual Packaging".
So when the PHP script processes the checkbox array, the array only contains
which boxes were checked. So let's say I had checked four of the items ...
I do a foreach through the checkbox array and build a string ...
The values of each checkbox separated by pipes |
I save that in the table column (or field) you might call "specialties".
Any script that needs to know what specialties they selected, you
explode the string (delimiter is pipe |) and now you know which checkboxes
If you add more checkboxes to your "specialties" list, it won't affect anything.
Right now you have 11 items ... but say you add 5 more. When I fill out your
form, my list of checkboxes might look like this:
So now you would know which checkboxes they selected.
When you render the form once more, as I mentioned before, you explode
the "specialties" string and using "if" statements in your HTML form, you decide
if the checkbox tag should have the checked="checked" property or not.
That's the way I do it.
I like the idea of only using 1 column in my table for the whole checkbox array.
It seems to make it easier to troubleshoot as well.