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 5 of 5

Thread: Checkbox array

  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checkbox array

    Hi,

    I have a form with checkbox array:

    <input type="checkbox" name="specialties[]" value="bla">
    <input type="checkbox" name="specialties[]" value="bla bla">

    Once the form is submited, I am trying to explode the array so I can use the "selected" option on the checkboxes when manditory fileds are missed.

    howto?

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    There is no 'selected' attribute in php, checkboxes are not considered objects per se, but instead a single entity.
    So, to retrieve your values would be via $_POST/$_GET['specialities']. The specialites index is a type of array and can be iterated through. Each value should correspond to a value that has been checked:
    Code:
    array(
      'specialities' => array(
        [0] => 'bla',
        [1] => 'bla bla'
      )
    )
    So you can explode the values if you want: $vals = explode($_POST/_GET['specialities']);
    Would be the value of the _REQUEST['specialties'] array if both of these options are selected. Be warned, if only one option is selected, the value will still be an array as you have specified in the html as it being such.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    I tried:

    PHP Code:
        if (is_array($_POST['special'])) {
        
    $a implode(","$_POST['special']);
        }

    <
    input type="checkbox" name="special[]" '.( $a=='test' ? 'selected' : '' ).' value="test"
    Remains unselected. Is that the right concept?

    Thanks

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yes, thats incorrect, remember that implode will put your pieces together, so you need to check it on each individual item.
    Just check to see if its in the array:
    PHP Code:
    $checked = array();
    if (!
    is_array($_POST['special']))
    {
         
    $checked $_POST['special'];
    }
    // Don't know what else you want to do with this, so we'll just shove it in a var:
    $checkbox '<input type="checkbox" name="special[]" value="test" ' in_array('test'$checked) ? 'checked="checked"' '' '/>'
    To shorten it up, you may want to stick all of the checkbox values into an array as well:
    PHP Code:
    $aChkVals = array(
        
    'test',
        
    'test2',
    );
    $checkboxes '';
    foreach (
    $aChkVals AS $chkVals)
    {
        
    $checkboxes .= // All that input stuff here

    Kill two birds with one stone
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder
    Join Date
    Feb 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Im not really following ...

    I have alot of check boxes .. ie:

    PHP Code:
        <td bgcolor="#F2F5F9"><input type="checkbox" name="specialties[]" value="Retail/Shopping"></td>
        <
    td align="left" bgcolor="#F2F5F9" class="small_10">Retail/Shopping</td>
        <
    td bgcolor="#F2F5F9"><input type="checkbox" name="specialties[]" value="Solar Design"></td>
        <
    td align="left" bgcolor="#F2F5F9" class="small_10">Solar Design</td>
        <
    td bgcolor="#F2F5F9"><input type="checkbox" name="specialties[]" value="Sports/Athletic Facilities"></td>
        <
    td align="left" bgcolor="#F2F5F9" class="small_10">Sports/Athletic Facilities</td
    And dont want to break out of the echo ..

    Thanks


  •  

    Posting Permissions

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