Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.



    .


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •