assign selected option value from database

前端 未结 3 435
花落未央
花落未央 2021-01-28 13:38

I have a form which contains a dropdown list of countries generated from database. The values are stored in database. There is an option in which user can view or update the val

相关标签:
3条回答
  • 2021-01-28 13:45

    $selected = $list["country_country_name"];

     <tr><td>Country</td>
     <td>
     <select onchange="getCountry(this.value);" name="country" id="country" >
     <?php  foreach( $query as $qry ) { 
        $sel = '';
       if( $qry["country_country_name"] == $selected ) 
        $sel = 'selected="selected"';           
    
        echo '<option value="'.$qry["country_country_name"].'" '.$sel.'>'.$qry["country_country_name"].'</option>'."\n";
        } ?>
        </select>
        <?php echo form_error('country'); ?>
        </td>
     </tr>
    
    0 讨论(0)
  • 2021-01-28 13:45
    <select id="list" name="list">
        <option value=""> Please Select </option>
        <?
            $list = array('1',
                            '2',
                            '3',
                            '4',
                            '5',
                            '6');
    
            while ($L = array_shift($list)) {
                ?>
                    <option value="<?=$L?>" <? if($selected == $L){ echo 'selected="selected"'; }?> > 
                        <?= $L ?> 
                    </option> 
                <?
             }
        ?>
    </select>
    

    you can simple get the selected option with this:

    $("#list").val();
    

    try please.

    0 讨论(0)
  • 2021-01-28 13:58

    When your option tag's value attribute contains the same value as the option's text, the value attribute is not necessary -- so omit it.

    Below shows an inline condition statement.

    <tr>
        <td>Country</td>
        <td><select onchange="getCountry(this.value);" name="country" id="country">
            <?php
            foreach ($query as $row) { 
                echo "<option" ,
                    ($row["country_country_name"] == $list['country_country_name'] ? ' selected' : '') ,
                    "{$row['country_country_name']}</option>\n";
            }
            ?>
        </select></td>
    </tr>
    
    0 讨论(0)
提交回复
热议问题