Results 1 to 2 of 2

Thread: getting all values from a checkbox with same name

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    getting all values from a checkbox with same name

    I have a form with items dynamically created and they all are given the same name but different values. When I read the name on the recieving page I only get the last value that was checked. How do I read all values passed if checked?

    Here is my form code:
    <form action="comparetest.php" method="get" name="frmTest" id="frmTest">
    <table width="1" border="0">
    <tr>
    <td>Selection1</td>
    <td><input name="chkCompare" type="checkbox" id="chkCompare" value="1"></td>
    </tr>
    <tr>
    <td>Selection2</td>
    <td><input name="chkCompare" type="checkbox" id="chkCompare" value="2"></td>
    </tr>
    <tr>
    <td>Selection3</td>
    <td><input name="chkCompare" type="checkbox" id="chkCompare" value="3"></td>
    </tr>
    <tr>
    <td><input type="submit" name="Submit" value="Submit"></td>
    <td>&nbsp;</td>
    </tr>
    </table>
    <p>&nbsp; </p>
    </form>

    Thanks for any help or tips.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You need to set it up like this:

    Code:
    <input name="chkCompare" type="checkbox" id="chkCompare[]" value="1">
    So that when it's sent to a php script, it's sent as an array.

    Then in PHP:

    Code:
    for ($i = 0; $i < count($_GET['chkCompare']); $i++) {
        echo $_GET['chkCompare'][$i];
    }
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width