在更新中,如何从选择框中选择一个选项?

时间:2013-06-19 19:38:52

标签: php html5 cakephp cakephp-2.3

下午好,

使用cakephp,如何在更新操作中的选择框中选择一个值?

在视图中我得到像这样的变量:

<?php $category = $itemEditar['Anuncio']['category']; ?>

我需要从选择框中选择一个选项:

<select id="select_category" name="enquiry">
                    <option value="" >selecione</option>
                    <option value="Category1">Categoria1</option>
                    <option value="Category2">Categoria2</option>
                    <option value="Category3">Categoria2</option>
                </select>

要进行更新操作,我需要标记数据库中保存的类别,但我没有得到如何执行此操作。

2 个答案:

答案 0 :(得分:2)

您想要针对每个选项或选项检查$ category,如果匹配则设置所选属性。

<select id="select_category" name="enquiry">
    <option value="" >selecione</option>
    <option<?= $category == "Category1"?" selected = 'selected'":"" ?> value="Category1">Categoria1</option>
    <option<?= $category == "Category2"?" selected = 'selected'":"" ?> value="Category2">Categoria2</option>
    <option<?= $category == "Category3"?" selected = 'selected'":"" ?> value="Category3">Categoria2</option>
</select>

答案 1 :(得分:0)

正确的答案是:

<select id="select_category" name="enquiry" >
                    <option value="Category1" <?php echo $category == "Category1"?" selected = 'selected'":"" ?> >Categoria1</option>
                    <option value="Category2" <?php echo $category == "Category2"?" selected = 'selected'":"" ?> >Categoria2</option>
                    <option value="Category3" <?php echo $category == "Category3"?" selected = 'selected'":"" ?> >Categoria2</option>
                </select>
相关问题