2010-06-29 28 views
1

使用Php我寫下了下拉列表框代碼。 編輯員工詳細信息我已經使用相同的代碼,並試圖設置列表框中的值,但未設置該值。這個怎麼做。如何設置值爲<select>標籤

Employee Name: <select name=emp_name *$value='$name'*> 
<option> -- EMPLOYEE NAME -- </option> 
<? 
    while ($q->fetchInto($row)){ 
     print "<option>$row[0]</option>"; 
    } 
?> 
</select> 

回答

4

必須設置選定的選項選中,同時建立像這樣的選項:

<select name='emp_name'> 
    <? 
    //Define the array of possible options. 
    //This can come from the database fetch such as: 
    $options = $q->fetchMutipleVals(); 
    //Or defined manually such as: 
    $options = array('value1'=>'label1', 'value2'=>'label2'); //etc. 
    foreach ($options as $val => $label){ 
     $selected = ($name == $val)? "selected='selected'":''; //sets html flag 
     echo "<option value='$val' $selected>$label</option>\n"; 
    } 
    ?> 
</select> 
1

您需要option標籤與您想選擇具有selected屬性:

<select> 
    <option> -- EMPLOYEE NAME -- </option> 
    <option>Sally</option> 
    <option selected="selected">Fred</option> 
    <option>Liz</option> 
</select> 
0

嘗試

Employee Name: < select name=emp_name $value='$name' > < option > -- EMPLOYEE NAME -- </option > 

< ? while ($q->fetchInto($row)) 

{ print "<option>$row[0]</option>"; } </select > 

? > 
2

檢驗正確的價值,你遍歷並標記<option>你想通過使用selected='selected'屬性將其作爲選定值:

Employee Name: <select name="emp_name"> 
<option> -- EMPLOYEE NAME -- </option> 
<?php 
    while ($q->fetchInto($row)){ 
     if($row[0] == $name){ 
      $selected_text = " selected='selected'"; 
     }else{ 
      $selected_text = ""; 
     } 
     print "<option{$selected_text}>{$row[0]}</option>"; 
    } 
?> 
</select> 
相關問題