2017-03-28 68 views
1

我使用php存儲過程在sql服務器上。我的問題是,我想在下拉列表中顯示一個值,但是當某人選擇了其他內容和頁面刷新時,它必須顯示該人選擇的內容,而不是默認值。這裏我到目前爲止所做的:php下拉菜單顯示選中,但一個值由defaut

<form method="post"> 
      <label for="from"><b>De:</b></label> 
      <input data-date-format='yy-mm-dd' type="text" id="from" name="from" size="6" value="<?php if(isset($_POST['from'])){ echo $_POST['from']; } ?>";/> 
      <label for="to"><b>A:</b></label> 
      <input data-date-format='yyyy-mm-dd' type="text" id="to" name="to" size="6" value="<?php if(isset($_POST['to'])){ echo $_POST['to']; } ?>";/> 
      <label for="group"><b>Groupe:</b></label> 
      <select type= "text" name="group" id="group" value="";> 
      <option><?php if(isset($_POST['group'])){ echo $_POST['group']; } ?></option> 
<?php 
$sql = " Get_List_ReportStation"; 
$stmt = sqlsrv_query($conn, $sql); 
if($stmt === false) { 
    die(print_r(sqlsrv_errors(), true));} 
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) 
{ 
?>   
<option selected value="RAL"><?php echo ($row['F1000'] . " " . $row['F1018']); ?></option>  
<?php 
} 
?> 
</select> 
<input type="submit" value="Valider" name="submit"> 
</form> 
<?php 
if(isset($_POST["submit"])) { 
$from = $_POST["from"]; 
$to = $_POST["to"]; 
$group = $_POST["group"]; 
} 
?> 

任何想法來幫助我?謝謝!

回答

1
<select class="group"> 
    <option value="test1">test1</option> 
    <option value="test2">test2</option> 
    <option value="test3">test3</option> 
    <option value="test4">test4</option> 
</select>  

添加Javascript將用戶最後一個值存儲到localstorage中。

var select = document.querySelector(".group"); 
var selectOption = select.options[select.selectedIndex]; 
var lastSelected = localStorage.getItem('select'); 

if(lastSelected) { 
    select.value = lastSelected; 
} 

// sets up a onselected 
select.onchange = function() { 
    lastSelected = select.options[select.selectedIndex].value; 
    console.log(lastSelected); 
    localStorage.setItem('select', lastSelected); 
} 

來源:How to use LocalStorage on last HTML select value

了給定的代碼試試這個

<form method="post"> 
      <label for="from"><b>De:</b></label> 
      <input data-date-format='yy-mm-dd' type="text" id="from" name="from" size="6" value="<?php if(isset($_POST['from'])){ echo $_POST['from']; } ?>";/> 
      <label for="to"><b>A:</b></label> 
      <input data-date-format='yyyy-mm-dd' type="text" id="to" name="to" size="6" value="<?php if(isset($_POST['to'])){ echo $_POST['to']; } ?>";/> 
      <label for="group"><b>Groupe:</b></label> 
      <select type= "text" name="group" id="group"> 
      <option><?php if(isset($_POST['group'])){ echo $_POST['group']; } ?></option> 
<?php 
$sql = " Get_List_ReportStation"; 
$stmt = sqlsrv_query($conn, $sql); 
if($stmt === false) { 
    die(print_r(sqlsrv_errors(), true));} 
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) 
{ 
?>   
<option value="<?php echo ($row['F1000']."_".$row['F1018']); ?>"><?php echo ($row['F1000'] . " " . $row['F1018']); ?></option>  
<?php 
} 
?> 
</select> 
<input type="submit" value="Valider" name="submit"> 
</form> 
<script> 
var select = document.querySelector(".group"); 
var selectOption = select.options[select.selectedIndex]; 
var lastSelected = localStorage.getItem('select'); 

if(lastSelected) { 
    select.value = lastSelected; 
} else { 
    select.value = 'RAL'; // new code 
} 

// sets up a onselected 
select.onchange = function() { 
    lastSelected = select.options[select.selectedIndex].value; 
    console.log(lastSelected); 
    localStorage.setItem('select', lastSelected); 
} 
</script> 
<?php 
if(isset($_POST["submit"])) { 
$from = $_POST["from"]; 
$to = $_POST["to"]; 
$group = $_POST["group"]; 
} 
?> 
+0

是的,我明白,而是來自我的存儲過程我的信息,所以我不知道會出現什麼在下拉菜單中。 –

+0

您的JavaScript將在您的存儲過程通過php將信息填充到您的html之後運行。一旦生成html,你的客戶端將接收到html + js並運行js。此代碼是獨立於您的下拉列表中的值:) –

+0

Sorrry,我是初學者。我不太確定。如果我從我的存儲過程中獲取信息:?php echo($ row ['F1000']。「」。$ row ['F1018']); ?並在我的選項中選擇了「RAL」值。 RAL是默認值,但如果別人拿別的東西,然後我打電話:php if(isset($ _ POST ['group'])){echo $ _POST ['group'];在哪裏我必須改變做你說的話並保持相同的結果?再次感謝你! –

相關問題