PHP Checkboxes

Discussion in 'Web Design and Development' started by vendettabass, Aug 5, 2008.

  1. vendettabass macrumors 6502a

    vendettabass

    Joined:
    Jul 8, 2006
    Location:
    Seoul, South Korea
    #1
    hey guys, I've scoured the internet and found no resolution to my problem :-(.

    Basically I'm trying to add members (read from a database) to an event. I'm using checkboxes for the admin to select multiple users to add to the event. (Note: the <form> is further up the code).

    while($row = mysql_fetch_array($result,MYSQL_ASSOC))
    {
    echo "<tr class='even'><td>".$row[name]."</td><td><input name='members[]' value='".$row[id]."' type='checkbox' /></td>";
    }
    echo "</table>";
    echo "<p><input type='submit' value='Complete Event' id='submit' /></p>";
    echo "</form>";


    The $row[id] are numbers, and what I need are these numbers in an array on the next page so I can use them. I just can't figure out how to get these numbers.

    Any idea what syntax I'll need to obtain each item from the array?

    thanks a lot
     
  2. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #2
    When the form is submitted, if you used post method:

    $_POST['members']

    If get method:

    $_GET['members']

    Each will be an array, so to iterate through, one of million ways, but easy:

    PHP:
    foreach ($_POST['members'] as $MemberID) {
    echo 
    "A user with ID number $MemberID was selected<br />";
    }
    Good job using [] on the end of the name field in your input tag for the checkboxes. Many people forget to do that in PHP - doing so easily creates an array in your $_POST or $_GET global variable which PHP creates for you. Well done!

    -jim
     

Share This Page