faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input : Array Form Variables

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

133 of 173 people (77%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How to do a select multiple?

Nov 18th, 2006 04:47
yoss chang, Nathan Wallace, Ryan Seager, Onno Benschop, Nick Talbott


The principle is to use an array name as the variable name in the
<select> tag  -  in your case 'foo[]'.  Here's a complete self-contained
example I posted to this list last year.

    <HTML><HEAD></HEAD>
    <BODY>

    <H2>Using multiple select</H2>

    <?php
    $num_selected = count($sel);

    if ($num_selected)
    {
        print "Number of options selected: $num_selected<p>\n";

        print_r($_POST['sel']);
    }
    ?>

    <form method=post>

    <select name=sel[] multiple size=3>
      <option value=red>Red
      <option value=green>Green
      <option value=blue>Blue
      <option value=brown>Brown
      <option value=black>Black
    </select>

    <br>
    <input type=submit value="Submit">

    </form>

    </BODY></HTML>