如何在php中获取选择框的多个选定值?

问题描述:

我有一个html表单,它有一个选择列表框,您可以从中选择多个值,因为它的multiple属性设置为多个。考虑表单方法是GET。表单的html代码如下:

I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows:

<html>
    <head>
    <title>Untitled Document</title>
    </head>
    <body>
    <form id="form1" name="form1" method="get" action="display.php">
      <table width="300" border="1">
        <tr>
          <td><label>Multiple Selection </label>&nbsp;</td>
          <td><select name="select2" size="3" multiple="multiple" tabindex="1">
            <option value="11">eleven</option>
            <option value="12">twelve</option>
            <option value="13">thirette</option>
            <option value="14">fourteen</option>
            <option value="15">fifteen</option>
            <option value="16">sixteen</option>
            <option value="17">seventeen</option>
            <option value="18">eighteen</option>
            <option value="19">nineteen</option>
            <option value="20">twenty</option>
          </select>
          </td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
        </tr>
      </table>
    </form>
    </body>
    </html>

在display.php页面上选择列表框。那么在display.php页面上如何使用 $ _ GET [] 数组访问所选值。

I want to display the selected values in select list box on display.php page. So how are the selected values accessed on display.php page using $_GET[] array.

如果您希望PHP将 $ _ GET ['select2'] 视为一个选项数组,只需将方括号添加到选择元素,如下所示:< select name =select2 []multiple ...

If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …

然后您可以在PHP脚本中访问数组

Then you can acces the array in your PHP script

<?php
header("Content-Type: text/plain");

foreach ($_GET['select2'] as $selectedOption)
    echo $selectedOption."\n";

$ _ GET 可以由 $ _ POST 取决于< form method =...值。