简体   繁体   中英

in an update, how to select an option from a select box?

Good afternoon,

Using cakephp, how can I select a value in a select box in an update operation?

in the view I get the variables like this for example:

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

and I need to select an option from a select box:

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

to be an update operation, I need to mark the category that was saved in the database, and am not getting how to do this.

You want to check the $category against each or the options and if they match set the selected attribute .

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

the correct answer is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM