2013-06-19 37 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> 
相關問題