提交后如何显示选定的选项值
How to show selected option value after submit
我有一个 html select,其中填充了来自使用 foreach 循环的查询的数据。默认值为空,因此当页面加载时,它会显示一个未过滤的查询。看起来像这样
$client = $wpdb->get_results("SELECT string FROM `file` WHERE
`code` = 001");
echo 'Filter by client: ';
echo '<select name="client_list"><option value=""></option>';
foreach ($client as $key => $row) {
$value = $row->string;
echo
'<option value='.$value.'>'
.$value. '</option>';
}
$client = $_GET['client_list'];
echo '</select>';
它作为过滤器显示基于 selected 选项值的数据。它过滤的 table 看起来像这样
|client | file |
|------ |-------------------|
|client1 | file00000 |
|client2 | file00002 |
现在,当我点击提交并看到正确的筛选查询结果时,我还看到了默认选项值,而不是 selected 用于筛选 html select。我该如何修复 this?
保留 $_POST 变量中的值,然后添加要保留的所选属性并将其设置为所选值:
echo 'Filter by client: ';
echo '<select name="client_list"><option value=""></option>';
foreach ($client as $key => $row) {
$value = $row->string;
if($_GET['client_list'] == $value){
echo '<option value='.$value.' selected>'.$value. '</option>';
}else{
echo '<option value='.$value.'>'.$value. '</option>';
}
$client = $_GET['client_list'];
echo '</select>';
我有一个 html select,其中填充了来自使用 foreach 循环的查询的数据。默认值为空,因此当页面加载时,它会显示一个未过滤的查询。看起来像这样
$client = $wpdb->get_results("SELECT string FROM `file` WHERE
`code` = 001");
echo 'Filter by client: ';
echo '<select name="client_list"><option value=""></option>';
foreach ($client as $key => $row) {
$value = $row->string;
echo
'<option value='.$value.'>'
.$value. '</option>';
}
$client = $_GET['client_list'];
echo '</select>';
它作为过滤器显示基于 selected 选项值的数据。它过滤的 table 看起来像这样
|client | file |
|------ |-------------------|
|client1 | file00000 |
|client2 | file00002 |
现在,当我点击提交并看到正确的筛选查询结果时,我还看到了默认选项值,而不是 selected 用于筛选 html select。我该如何修复 this?
保留 $_POST 变量中的值,然后添加要保留的所选属性并将其设置为所选值:
echo 'Filter by client: ';
echo '<select name="client_list"><option value=""></option>';
foreach ($client as $key => $row) {
$value = $row->string;
if($_GET['client_list'] == $value){
echo '<option value='.$value.' selected>'.$value. '</option>';
}else{
echo '<option value='.$value.'>'.$value. '</option>';
}
$client = $_GET['client_list'];
echo '</select>';