php从列表框

时间:2016-11-16 12:15:15

标签: php html5

当我在列表框中选择多个设备并按下提交时,我有一个PHP表单如下所示,我在后端php脚本中只看到一个值。

在此列表框中,我有几个值,但只有一个传递给后端脚本。 Form with multiple values in list box

PHP表单:

 <br><br>
    Select Devices:<br>
    <?php
<form target="iframe_b" action="/php_src/sendNIDDConfigReq.php" method="POST"
        echo "sending data">
  <fieldset>

    <legend style="font-size:150%"><b>send NIDDRequest</b></legend>
    <br> <br>
 Select SCEF:<br>
    <?php

$dbhost = 'localhost:3036';
    $dbuser = 'root';
    $dbpass = 'XXXX';
    $conn   = mysql_connect($dbhost, $dbuser, $dbpass);
    if (!$conn) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("ApplicationServer") or die(mysql_error());
    // Get all the data from the "example" table
    $result = mysql_query("SELECT EXTERNAL_ID FROM DEVICE_DETAILS") or die(mysql_error());
   echo "<select name='External_ID' id='id_extID'  multiple='multiple'>";
while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['EXTERNAL_ID'] . "'>" . $row['EXTERNAL_ID'] . "</option>";
}
echo "</select>";


send
?>
   <br><br>
    <input type="button" id="select_all" name="select_all" value="Select All">
    <input type="button" id="de_select_all" name="de_select_all" value="DeSelect All">
    <br><br>
    <input type="submit" value="Submit">
</fieldset>

</form>
<script type="text/javascript">

$('#select_all').click(function() {
        $('#id_extID option').prop('selected', true);
    });


$('#de_select_all').click(function() {
        $('#id_extID option').prop('selected', false);
    });
</script>

服务器代码:sendNIDDConfigReq.php

print_r($_POST);
foreach ($_POST["EXTERNAL_ID"] as $selectedOption)
{
    echo $selectedOption."\n";
}

输出: 数组([SCEF_Name] =&gt; SCEF1 [External_ID] =&gt; 123@mydomain.com)SCEF_Name = SCEF1 External_ID=123@mydomain.com

2 个答案:

答案 0 :(得分:3)

当您有多个选择下拉列表时,名称应为&#39; []&#39;像这样附上。

echo "<select name='External_ID[]' id='id_extID'  multiple='multiple'>";

答案 1 :(得分:0)

使用输入元素名称作为数组,例如External_ID []