SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checkbox array in form

    Im trying to create a form with multiple checkboxes. I would like to create one array instead of tons of variables. Just to save time when coding these forms. I also would like the form to be sticky. I will email the array value. Could someone help with the code for this? Thanks

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Heres an example

    HTML Code:
    <form action="" method="">
        <input type="checkbox" name="my_checkbox[]" value="something 1" />
        <input type="checkbox" name="my_checkbox[]" value="something 2" />
        <input type="checkbox" name="my_checkbox[]" value="something 3" />
        <input type="checkbox" name="my_checkbox[]" value="something 4" />
        <input type="checkbox" name="my_checkbox[]" value="something 5" />
    </form>
    PHP Code:
    if (isset($_POST['my_checkbox']) && is_array($_POST['my_checkbox']) && sizeof($_POST['my_checkbox']) > 0){
        foreach(
    $_POST['my_checkbox'] as $checkbox){
            echo 
    $checkbox.'<br />';
        }
    } else {
        echo 
    'Please check at least "1" checkbox!';



Bookmarks

Posting Permissions

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