如何选择默认值下拉菜单

时间:2015-02-10 05:56:40

标签: php jquery html mysql drop-down-menu

我想在下拉菜单中动态选择默认值 该值存储在由$row['order status']检索的mysql服务器上 它只有4个值存储在服务器Open Pending Closed Success

所以我从这4个值

中选择了默认值
$selected are all initialised to false or '0' or disabled

我正在尝试以下代码,但它无法正常工作

 if($row["Order Status"]=="Open"){ 
       $selected='selected';
    }
    if($row["Order Status"]=="Pending"){
       $selected1='selected';
    }
    if($row["Order Status"]=="Closed"){
       $selected2='selected';
    }
    if($row["Order Status"]=="Success"){
       $selected3='selected';
    }   
    echo"
    <select id='txt_orderstatusid_'  class='txt_id_' >
       <option ".$selected." value='Open'>Open </option>
       <option ".$selected1." value='Pending'>Pending </option>
       <option ".$selected2."value='Closed'>Closed </option>
       <option ".$selected3."value='Success'>Success </option>
    </select>"

我也试过这个

echo "<select   id='txt_orderstatusid_'  class='txt_id_' >
<option selected=".$selected." value='Open'>Open </option>
<option selected=".$selected1." value='Pending'>Pending </option>
<option selected=".$selected2."value='Closed'>Closed </option>
<option selected=".$selected3."value='Success'>Success </option>
</select>   "

它也不起作用

1 个答案:

答案 0 :(得分:4)

您可以直接在选项中检查数据库值

<select id='txt_orderstatusid_'  class='txt_id_' >
    <option <?php if(isset($row["Order Status"]) && $row["Order Status"]=="Open") echo 'selected="selected"';?> value='Open'>Open </option>
    <option <?php if(isset($row["Order Status"]) && $row["Order Status"]=="Pending") echo 'selected="selected"';?> value='Pending'>Pending </option>
    <option <?php if(isset($row["Order Status"]) && $row["Order Status"]=="Closed") echo 'selected="selected"';?> value='Closed'>Closed </option>
    <option <?php if(isset($row["Order Status"]) && $row["Order Status"]=="Success") echo 'selected="selected"';?> value='Success'>Success </option>
 </select>