我在sql server上使用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"];
}
?>
有什么想法可以帮到我吗?谢谢!
答案 0 :(得分: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"];
}
?>