小编典典

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

html

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

<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页面的选择列表框中显示选定的值。那么如何使用$_GET[]数组在display.php页面上访问选定的值。


阅读 456

收藏
2020-05-10

共1个答案

小编典典

如果要将PHP $_GET['select2']视为选项数组,只需将方括号添加到 select元素 的名称中,如下所示:<select name="select2[]" multiple …

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

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

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

$_GET可以$_POST根据<form method="…"值替换。

2020-05-10