通过开机自检从选择中获取所有选项


Get all options from select via POST

我正在构建一个页面,其中有 2 个选择框。我添加项目以从选择#2中选择#1。

单击提交按钮时,无论是否选择,我只想从"选择#2"中获取所有选项。

<?php
require('handlers/handler.php');
$active = $_POST['activeProperties'];
$slider = $_POST['sliderProperties'];
$array = array();
if(isset($_POST['submit'])){
    if(isset($_POST['sliderProperties'])){
        foreach($_POST['sliderProperties'] as $item){
            $array[] = $item;
        }
    }
    echo "<pre>";
    print_r($array);
    echo "</pre>";
}
?>
<form method="post" style="height: 600px;">
            <select id="source" name="activeProperties[]" data-text="Source list" data-search="Search for options" style="height: 600px;">
                <?php 
                $query = $handler11->query("SELECT ID, name FROM properties WHERE active = 'yes' ");
                while($row = $query->fetch()){
                    echo '<option value="'.$row['ID'].'">'.$row['name'].'</option>';
                }
                ?>
            </select>
            <select id="destination" name="sliderProperties[]" data-text="Destination list"  data-search="Search for options">
            </select>
            <input type="submit" name="submit" class="btn btn-primary" value="Save"/>
    </form>

我只在使用此代码获得选择框中的第一项。如何获得所有物品?

您可以使用 HTML 的多选(文档在这里),试试这个:

<form method="post" style="height: 600px;">
    <select multiple id="source" name="activeProperties[]" data-text="Source list" data-search="Search for options" style="height: 600px;">
        <?php 
            $query = $handler11->query("SELECT ID, name FROM properties WHERE active = 'yes' ");
            while($row = $query->fetch()){
                echo '<option value="'.$row['ID'].'">'.$row['name'].'</option>';
            }
        ?>
    </select>
</form>

另一种选择是通过Javascript。

如果你想要所有的数据(如果选择与否),你是否通过这个查询在你的php中获得相同的结果:

$query = $handler11->query("SELECT ID, name FROM properties WHERE active = 'yes' " );