php 如何在Form POST或GET后保留选择框的选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2904932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Keep the selected value of the select box after Form POST or GET
提问by Rajasekar
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
例如,如果我在组合中选择类别“汽车”并单击搜索,则在表单提交后,组合应将汽车显示为默认选择选项。请帮我。任何帮助将不胜感激
回答by Mihai Iorga
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>";
}
?>
回答by Quentin
Assuming that by "combo" you mean "A regular select element rendering as a drop down menu or list box" and not "A combobox that is a combination of a drop down menu and free text input":
假设“组合”的意思是“作为下拉菜单或列表框呈现的常规选择元素”而不是“组合框是下拉菜单和自由文本输入的组合”:
When outputting the <option>elements, check the value against the submitted data in $_POST/ $_GETand output selected(in HTML) or selected="selected"(in XHTML) as an attribute of the option element.
输出<option>元素时,根据$_POST/ 中提交的数据检查值,$_GET并输出selected(在 HTML 中)或selected="selected"(在 XHTML 中)作为选项元素的属性。
回答by Vencolini
Here is the JQuery way I am using.
这是我使用的 JQuery 方式。
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
$("#name").val("<?php echo $_POST['name'];?>");
</script>
But this is only if you have jquery included in your webpage. Regards
但这仅当您的网页中包含 jquery 时。问候
回答by user4447655
<?php
$example = $_POST["friend"];
?>
<form method="POST">
<select name="friend">
<option value="tom" <?php if (isset($example) && $example=="tom") echo ' selected';?>>Thomas Finnegan</option>
<option value="anna" <?php if (isset($example) && $example=="anna") echo ' selected';?>>Anna Karenina</option>
</select>
<br><br>
<input type="submit">
</form>
This solved my problem.
这解决了我的问题。
回答by Mehek Butt
Easy solution: If select box values fetched from DB then to keep selected value after form submit OR form POST
简单的解决方案:如果选择从数据库中获取的框值,则在表单提交或表单 POST 后保留所选值
<select name="country" id="country">
<?php $countries = $wpdb->get_results( 'SELECT * FROM countries' ); ?>
<option value="">
<?php if(isset($_POST['country'])){echo htmlentities($_POST['country']); } else { echo "Select Country *"; }?>
</option>
<?php foreach($countries as $country){ ?>
<option <?php echo ($_POST['country'] == $country->country_name ? 'selected="selected"':''); ?> value="<?php echo $country->country_name; ?>"><?php echo $country->country_name; ?>
</option>
<?php } ?>
</select>
回答by Mehek Butt
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
尝试后,这个“解决”没有任何工作。之前对w3school做了一些研究,记得有关于保持无线电价值观的解释。但它也适用于 Select 选项。请参阅此处的示例。试试吧,玩玩吧。
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
回答by Rajasekar
This Solved my Problem. Thanks for all those answered
这解决了我的问题。感谢所有回答的人
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
回答by Alvin567
$countries_uid = $_POST['countries_uid'];
while($row = mysql_fetch_array($result)){
$uid = $row['uid'];
$country = $row['country_name'];
$isSelected = null;
if(!empty($countries_uid)){
foreach($countries_uid as $country_uid){//cycle through country_uid
if($row['uid'] == $country_uid){
$isSelected = 'selected="selected"'; // if the option submited in form is as same as this row we add the selected
}
}
}else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$uid."'".$isSelected.">".$country."</option>";
}
this is my solutions of multiple select dropdown box after modifying Mihai Iorga codes
这是我修改Mihai Iorga代码后多选下拉框的解决方案

