2013-03-05 73 views
0
<form method="post"> 
    <select name="select_employee" id="select_employee"> 
    <?php $allemp=$this->AllEmployees; ?> 
    <option selected value="">Select an employee..</option> 
    <?php foreach($allemp as $row){ echo "<option value=".$row[ 'Id']. ">".$row[ 'Etunimi']. " - ". $row[ 'Sukunimi']. "</option>"; } ?> 
    </select> 
    <input type="hidden" name="send" value="namesent"> 
    <input type="submit" value="submit" id="button"> 
</form> 
<br/> 
<br/> 

選擇當我提交這種形式,我留在同一頁上,但我希望有一個相同的時間內保持選定的選項前面的選項可見。我怎麼能這樣做? 我想:保存選擇從下拉框中

document.getElementById('select_employee').value = "<?php echo $_GET['select_employee'];?>"; 

但它沒有工作..

回答

0

試試這個:

$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':''; 
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>'; 
  1. 添加上述echo options線210

  2. 在側面選項中增加'.$selected.'

您的代碼就變成了:複製粘貼下面的代碼,而不是您現在

<form method="post"> 
    <select name="select_employee" id="select_employee"> 
    <?php $allemp=$this->AllEmployees; ?> 
    <option selected value="">Select an employee..</option> 
    <?php foreach($allemp as $row){ 
     $selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':''; 
    echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>'; 
} ?> 
    </select> 
    <input type="hidden" name="send" value="namesent"> 
    <input type="submit" value="submit" id="button"> 
</form> 
<br/> 
<br/> 
+0

像魅力一樣工作謝謝你! – 2013-03-05 13:14:11

+0

@MNaibibaaziz:酷:D – 2013-03-05 13:14:53

1

這裏:

foreach($allemp as $row){ 
    echo "<option " . (isset($_POST['select_employee']) && $_POST['select_employee'] == $row['Id'] ? ' selected ' : '') . " value=".$row['Id'].">".$row['Etunimi']." - ".$row['Sukunimi']."</option>"; 
} 
+0

,我看不到任何選項了.. – 2013-03-05 12:53:06

+0

你明白ENY錯誤? – d4rkpr1nc3 2013-03-05 13:13:20

+0

沒有錯誤,但選項不再存在。 – 2013-03-05 13:14:50