我有一个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页面上的选定值呢?


当前回答

变化:

<select name="select2" ...

To:

<select name="select2[]" ...

其他回答

你也可以这样做。这对我来说很有效。

<form action="ResultsDulith.php" id="intermediate" name="inputMachine[]" multiple="multiple" method="post">
    <select id="selectDuration" name="selectDuration[]" multiple="multiple"> 
        <option value="1 WEEK" >Last 1 Week</option>
        <option value="2 WEEK" >Last 2 Week </option>
        <option value="3 WEEK" >Last 3 Week</option>
         <option value="4 WEEK" >Last 4 Week</option>
          <option value="5 WEEK" >Last 5 Week</option>
           <option value="6 WEEK" >Last 6 Week</option>
    </select>
     <input type="submit"/> 
</form>

然后从下面的PHP代码中选择多个选项。它相应地打印所选的多个值。

$shift=$_POST['selectDuration'];

print_r($shift);

如果你想让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="…"值。

变化:

<select name="select2" ...

To:

<select name="select2[]" ...
foreach ($_POST["select2"] as $selectedOption)
{    
    echo $selectedOption."\n";  
}

这将显示所选的值:

<?php

    if ($_POST) { 
        foreach($_POST['select2'] as $selected) {
            echo $selected."<br>";
        }
    }

?>