2017-03-28 28 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'];在哪里我必须改变做你说的话并保持相同的结果?再次感谢你! –

相关问题