首页 > 解决方案 > 选择字段中的必需标签不起作用

问题描述

我尝试将所需的属性添加到我的选择标签中。不幸的是,这被忽略了。下拉列表由数据库条目动态填充。有谁知道为什么?是动态元素的错吗?

<div class="input-field col s12 m6 l6">
    <?php
    //-------------------------------------------//
    $statement = $pdo->prepare("SELECT * FROM availableCards WHERE Active = '1' ORDER BY Description");
    $statement->execute();
    echo '<select name="images" id="images" required>';
    echo '<option value="" disabled selected>Bitte wählen</option>';
    while ($data = $statement->fetchAll(PDO::FETCH_ASSOC)) {
        foreach ($data as $row) {
            echo '<option name="' . $row['Value'] . '" value="' . $row['Value'] . '" data-icon="../cards/' . $row['ImagePath'] . '">' . $row['Description'] . '</option>';
        }
    }
    echo '</select>';
    //-------------------------------------------//
    ?>
    <label for="images">Karte auswählen</label>
</div>

标签: phphtmlselectoption

解决方案


复制并粘贴以下块。您拥有的表单可能没有正确嵌套。

<form action="/">
<div class="input-field col s12 m6 l6">
    <?php
    $data = array(
        '1' => 'One',
        '2' => 'Two',
        '3' => 'Three',
    );
    //-------------------------------------------//

    echo '<select name="images" id="images" required>';
    echo '<option value="" disabled selected>Bitte wählen</option>';
    foreach ($data as $row) {
        echo '<option name="' . $row['Value'] . '" value="' . $row['Value'] . '" data-icon="../cards/' . $row['ImagePath'] . '">' . $row['Description'] . '</option>';
    }
    echo '</select>';
    //-------------------------------------------//
    ?>
    <label for="images">Karte auswählen</label>
    <button type="submit" name="button">Bestätigen</button>
</div>
</form>

请添加结束表单标签和最后一个 div 的。StackOverflow 的嵌入代码是一场噩梦。


推荐阅读