从数据库分配选定的选项值 [英] assign selected option value from database

查看:42
本文介绍了从数据库分配选定的选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中包含从数据库生成的国家/地区的下拉列表。值存储在数据库中。有一个选项,用户可以在其中查看或更新插入的值。为了更新所有表单值,从数据库中获取。我需要的是,在加载表格以更新国家下拉列表的所选选项时,必须是存储在数据库中的表格。
例如:如果从以下下拉菜单中选择了option2并将其插入数据库。

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 valuesinserted. For updating all the form values get fetched from database. What i require is that when form is loaded for updating the selected option of the country dropdown must be that stored in the database. For eg: if from the following dropdown option2 is selected and inserted into database.

 Dropdown: |option1|<selected>
           |option2|
           |option3|

在更新期间应该是这样

 Dropdown: |option1|
           |option2|<selected>
           |option3|

这是我尝试的代码。

      $selected = $list["country_country_name"];

     <tr><td>Country</td><td><select onchange="getCountry(this.value);" name="country" id="country" ><?php  foreach( $query as $qry ) { 
     print '<option value="'.$qry["country_country_name"].'"'; 
     if( $qry["country_country_name"] == $selected ) print'selected'; 
     print '>'.$qry["country_country_name"].'</option>'."\n";} ?>
     </select></td></tr>


推荐答案

$ selected = $ list [ country_country_name];

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

这篇关于从数据库分配选定的选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆