我有下面的代码,但是每次我选择一个值并且页面刷新下拉菜单中的值时,都会恢复为默认值,如何保存所选值。
我不能使用AJAX,因为用户不希望那样做。
我确实在线搜索,发现我需要添加“ selected”,但是当我在while语句上这样做时,它不起作用。
这是我到目前为止使用的代码
<h4>Please select a Scheme name</h4>
<br>
<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">
<select name="schemaNameSelect" id='schema_name_dropdown_menu'>
<option value='default' id='default' >Please select scheme name</option>
<option value='ViewAll' id='ViewAll' >View All</option></center>
<?php
while($dbRow = sqlsrv_fetch_array($dbQuery, SQLSRV_FETCH_ASSOC)) {
echo "<option value='".$dbRow[Schema_Name]."'>". $dbRow[Schema_Name] . " </option>";
}
echo "</select>";
}
?>
<a id="viewButton" type="submit"><button id="viewBtn">View</button></a>
<input type="submit" name="submit_docs" value="Export your certificate" id="exportBtn" class="input-button" />
<div id="test">
<br/>
<input id="create_schema_wrapper_input" name="schemaNameTextbox" data-id="12" class="stage3-input" placeholder="Please insert new schema name here">
</div>
</form>
<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">
</form>
<table class="table table-striped" id="student">
<tr>
<th>Schema Name</th><th>Stakeholder</th><th>Stakeholder Return</th><th>Schema Return</th>
</tr>
<?php
if (isset($_POST['schemaNameSelect'])) {
$schemaName = $_POST['schemaNameSelect'];
databaseOutput($schemaName);
} else {
databaseOutput("ViewAll");
}
?>
</table>
</body>
<style>
#schema_name_dropdown_menu{
padding: 5px;
font-size: 14px;
width: 350px;
height: 35px;
margin: 5px;
}
#test{
display: none;
}
#viewButton{
font-size: 13px;
text-decoration: none;
color: black;
display: none;
}
#viewButton:hover {
font-size: 15px;
color: blue;
text-decoration: none;
}
#viewBtn{
border-radius: 5px;
margin-left: 5px;
font-size: 14px;
width: 60px;
height: 35px;
margin: 5px;
}
#exportBtn{
margin: 5px;
height: 35px;
width: 150px;
font-size: 14px;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
$('#schema_name_dropdown_menu').change(function() {
if ($(this).val() == 'default')
{
$('#viewButton').hide();
}else{
$('#viewButton').show();
$('#test').hide();
var value = $("#schema_name_dropdown_menu option:selected").val();
$('#test').text(value);
}
});
</script>
答案 0 :(得分:1)
在填充下拉列表的while循环中,检查是否已选择下拉列表,然后将selected
属性添加到在while循环中找到的选项
<h4>Please select a Scheme name</h4>
<br>
<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">
<select name="schemaNameSelect" id='schema_name_dropdown_menu'>
<option value='default' id='default' >Please select scheme name</option>
<option value='ViewAll' id='ViewAll' >View All</option></center>
<?php
while($dbRow = sqlsrv_fetch_array($dbQuery, SQLSRV_FETCH_ASSOC)) {
// code to add the selected attribute
$sel = '';
if ( isset($_POST['schemaNameSelect'])
&& $_POST['schemaNameSelect'] == $dbRow[Schema_Name] )
{
$sel = 'selected';
}
echo "<option $sel value='".$dbRow[Schema_Name]."'>". $dbRow[Schema_Name] . " </option>";
// change ^^^
}
echo "</select>";
}
?>
<a id="viewButton" type="submit"><button id="viewBtn">View</button></a>
<input type="submit" name="submit_docs" value="Export your certificate" id="exportBtn" class="input-button" />
<div id="test">
<br/>
<input id="create_schema_wrapper_input" name="schemaNameTextbox" data-id="12" class="stage3-input" placeholder="Please insert new schema name here">
</div>
</form>
<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">
</form>
<table class="table table-striped" id="student">
<tr>
<th>Schema Name</th><th>Stakeholder</th><th>Stakeholder Return</th><th>Schema Return</th>
</tr>
<?php
if (isset($_POST['schemaNameSelect'])) {
$schemaName = $_POST['schemaNameSelect'];
databaseOutput($schemaName);
} else {
databaseOutput("ViewAll");
}
?>
</table>
</body>
答案 1 :(得分:0)
<select name="select">
<option <?=$variable1 == $variable2 ? 'selected' : ''?> value="1">One</option>
</select>