2013-06-19 34 views
0

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

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

是一个更新操作,我需要标记保存在数据库中的类别,并且不知道如何执行此操作。

+0

这不是很清楚,你要选择一个选项?或者你想获得一个选项的内容? –

+0

作为编辑操作数据的一种形式,我需要在选择框中设置一个选项。 该选项来自数据库。 –

+0

你的措辞不清楚。你想通过PHP填充你的选择价值? –

回答

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

这不就是Orangepill建议的只是php标签位置的一个小改动吗? – Nunser

+1

不要忘记select上的无效属性。 – Orangepill

+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> 
相关问题