Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-02-2012, 02:11 PM   PM User | #1
katmac
New Coder

 
Join Date: Aug 2012
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
katmac is an unknown quantity at this point
checkbox question

Hi

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?

Thanks.
katmac is offline   Reply With Quote
Old 12-02-2012, 05:37 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Quote:
checkboxes name is "specialties[]
.... what values do you give each checkbox?

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" ...
etc.

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 |

2|4|5|6|

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
they selected.

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:

5|6|11|13|14|

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.



.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:58 AM.


Advertisement
Log in to turn off these ads.