如何在提交后显示选定的选项值 [英] How to show selected option value after submit

查看:82
本文介绍了如何在提交后显示选定的选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用foreach循环来查询数据的html选择。默认值为空,因此当页面加载时,它会显示未经过滤的查询。它看起来像这样

  $ client = $ wpdb-> get_results(SELECT string FROM`file` WHERE 
`code` = 001);

echo'按客户端过滤:';
echo'< select name =client_list>< option value =>< / option>';
foreach($ client as $ key => $ row){
$ value = $ row-> string;
echo
'< option value ='。$ value。'>'
。$ value。 < /选项>;
}
$ client = $ _GET ['client_list'];
echo'< / select>';

它用作基于所选选项值显示数据的过滤器。它过滤的表看起来像这样

  | client |文件| 
| ------ | ------------------- |
| client1 | file00000 |
| client2 | file00002 |

现在,当我点击submit并查看过滤后的查询结果是正确的时,我也会看到默认选项值而不是选择的值来过滤html选择中的数据。如何修复这个

解决方案



 

保留$ _POST var的值,然后添加选定的属性以保留并将其设置为该选定值。 echo'按客户端过滤:';
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。 < /选项>;
} else {
echo'< option value ='。$ value。'>'。$ value。 < /选项>;
}
$ client = $ _GET ['client_list'];
echo'< / select>';


I have an html select that is populated with data from a query using a foreach loop. The default value is empty, so when the page loads, it displays an unfiltered query. It looks something like this

$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>'; 

It serves as a filter to display data based on the selected option value. The table that it filters looks something like this

   |client  | file              | 
   |------  |-------------------|
   |client1 | file00000         |
   |client2 | file00002         |

Now when I hit submit and see the filtered query results which are correct, I also see the default option value instead of the one selected to filter the data in the html select. How can I fix this?

解决方案

Retain the value from your $_POST var and then add the selected attribute to retain and have it set to that selected value:

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>'; 

这篇关于如何在提交后显示选定的选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆