选择字段中的必需标签无效

时间:2018-10-18 11:22:30

标签: php html select option

我尝试将必需的属性添加到我的选择标签中。不幸的是,这只是被忽略。下拉列表由数据库条目动态填充。有人知道为什么吗?应当谴责动态因素吗?

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

2 个答案:

答案 0 :(得分:1)

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

<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的嵌入代码是一场噩梦。

答案 1 :(得分:0)

实际上,在您的HTML值=“”处发生的不是在

中打印
<option disabled selected>Bitte auswählen</option>

但是在您的代码中,您已经添加了

echo '<option value="" disabled selected>Bitte wählen</option>';

如果您只关注为什么它没有在选项标签中显示value="",那么它将正常工作。

相关问题