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


当前回答

这将显示所选的值:

<?php

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

?>

其他回答

变化:

<select name="select2" ...

To:

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

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

<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);
    <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>

你可以像这样直接迭代

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

或者你可以这样做

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

您可以使用此代码从多个选择组合框中检索值

HTML:

<form action="c3.php" method="post">
  <select name="ary[]" multiple="multiple">
    <option value="Option 1" >Option 1</option>
    <option value="Option 2">Option 2</option>
    <option value="Option 3">Option 3</option>
    <option value="Option 4">Option 4</option>
    <option value="Option 5">Option 5</option>
  </select>
  <input type="submit">
</form>

PHP:

<?php
$values = $_POST['ary'];

foreach ($values as $a){
    echo $a;
}
?>