如何在Form POST或GET后保留选择框的选定值 [英] How to Keep the selected value of the select box after Form POST or GET

查看:137
本文介绍了如何在Form POST或GET后保留选择框的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网站中实施搜索功能。



当在文本框中输入搜索关键字并选择类别组合时,表单将被发布,结果将显示在同一页面上。 p>

我想要的是在默认情况下保留所选类别的组合。



对于eg。 ,如果我在组合中选择类别汽车并单击搜索,表单提交后,组合应该显示汽车作为默认选择选项。请帮帮我。

解决方案

我假设你从数据库获得类别。



您应该尝试:

 <?php 

$ categories = $ rows; //数组中的数组
foreach($ rows as $ row){
if($ row ['name'] == $ _POST ['category']){
$ isSelected =' selected =selected'; //如果在表单中提交的选项与此行相同,我们添加所选的标签
} else {
$ isSelected =''; // else we remove any tag
}
echo< option value ='。$ row ['id']。'。$ isSelected。>。$ row ['name ']。< / option>;
}
?>


Im trying to implement the search feature in my website.

when the search keyword is entered in the textbox, and the category combo is selected, the form will be Posted and the result will be shown on the same page.

what i want is to keep the selected category of the combo by default in the form after posted

For eg., If i select the category 'Automobiles' in the combo and click search, after form submit, the combo should show the automobiles as default selected option. Please help me. Any help will be appreciated

解决方案

I assume you get categories from database.

you should try:

<?php

$categories = $rows; //array from database
foreach($rows as $row){
     if($row['name'] == $_POST['category']){
          $isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
     } else {
          $isSelected = ''; // else we remove any tag
     }
     echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>

这篇关于如何在Form POST或GET后保留选择框的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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