在PHP中如何更新下拉列表值

时间:2018-06-22 04:42:23

标签: php

如何更新下拉列表值

<div class="form-group">

<label for="exampleSelect1" class="col-lg-2 control-label">Country</label>

<div class="col-lg-4">

<select class="form-control" id="exampleSelect1" value="<?php echo 

$raw['country'];?>" name="country" >

        <option>INDIA</option>
        <option>US</option>
        <option>UK</option>
        <option>BHUTAN</option>
        <option>CHINA</option>

      </select>

      </div>

    </div>

2 个答案:

答案 0 :(得分:0)

我认为您想显示来自database的下拉列表值。如果是这样,那么这里是解决方法:-

function get_countries(){   
$sql_get_countries = "select *                        
                      from ".main::$db_countries."                        
                      where continent_code = '".$_SESSION['continent_id']."'"             ;                       
$res_get_countries = mysql_query ($sql_get_countries) ; 
if (mysql_num_rows ($res_get_countries) > 0){
    $i = 0 ;
    while ($row_get_countries = mysql_fetch_object ($res_get_countries)){
        $tmp = array(
            'name'=>$row_get_countries->name,               
            'number'=>$row_get_countries->number
        );
        $this->get_countries[$i++] = $tmp ;
    }
}
return $this->get_countries ;
}

国家表中有洲代码。

现在是前端:-

$country_array = $mp->get_countries() ;

<div class="item form-group">

                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="country">Country<span class="required">*</span>

                    </label>

                    <div class="col-md-6 col-sm-6 col-xs-12">

                      <select class="form-control" id="country">

                        <option value="0">Choose Country</option>

                        <?php

                        foreach($country_array as $key=>$val){

                        ?>

                            <option value="<?=$val['number'];?>"><?=$val['name'];?></option>

                        <?php

                        }

                        ?>

                      </select>

                    </div>

                  </div>

答案 1 :(得分:0)

您需要传递选项值,然后需要设置正确的选项标签的所选属性:

<option value="INDIA" selected="selected">INDIA</option>

您的PHP看起来像这样:

尝试一下

<select class="form-control" id="exampleSelect1" name="country" >

  <option value="INDIA" <?php echo $raw['country'] == 'INDIA' ? ' selected="selected"' : ''; ?> >INDIA</option>
  <option value="US" <?php echo $raw['country'] == 'US' ? ' selected="selected"' : ''; ?> >US</option>
  <option value="UK" <?php echo $raw['country'] == 'UK' ? ' selected="selected"' : ''; ?> >UK</option>
  <option value="BHUTAN" <?php echo $raw['country'] == 'BHUTAN' ? ' selected="selected"' : ''; ?> >BHUTAN</option>
  <option value="CHINA" <?php echo $raw['country'] == 'CHINA' ? ' selected="selected"' : ''; ?> >CHINA</option>

</select>