php 如何在下拉框中设置所选项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1336353/
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 do I set the selected item in a drop down box
提问by tarnfeld
Is there any way to set the selected item in a drop down box using the following 'type' code?
有没有办法使用以下“类型”代码在下拉框中设置所选项目?
<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>
The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?
数据库保存一个月..我想在编辑页面上允许他们选择这个月..但是要预先填充他们当前的设置?
回答by Greg
You need to set the selected attribute of the correct option tag:
您需要设置正确选项标签的 selected 属性:
<option value="January" selected="selected">January</option>
Your PHP would look something like this:
你的 PHP 看起来像这样:
<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>
I usually find it neater to create an array of values and loop through that to create a dropdown.
我通常发现创建一组值并循环遍历以创建下拉列表更简洁。
回答by James Wheare
You mark the selected item on the <option>tag, not the <select>tag.
您在<option>标签上标记所选项目,而不是<select>标签。
So your code should read something like this:
所以你的代码应该是这样的:
<select>
<option value="January"<?php if ($row[month] == 'January') echo ' selected="selected"'; ?>>January</option>
<option value="February"<?php if ($row[month] == 'February') echo ' selected="selected"'; ?>>February</option>
...
...
<option value="December"<?php if ($row[month] == 'December') echo ' selected="selected"'; ?>>December</option>
</select>
You can make this less repetitive by putting all the month names in an array and using a basic foreachover them.
您可以通过将所有月份名称放在一个数组中并foreach在它们上使用基本名称来减少重复。
回答by Frederick Marcoux
You can use this method if you use a MySQL database:
如果您使用 MySQL 数据库,则可以使用此方法:
include('sql_connect.php');
$result = mysql_query("SELECT * FROM users WHERE `id`!='".$user_id."'");
while ($row = mysql_fetch_array($result))
{
if ($_GET['to'] == $row['id'])
{
$selected = 'selected="selected"';
}
else
{
$selected = '';
}
echo('<option value="'.$row['id'].' '.$selected.'">'.$row['username'].' ('.$row['fname'].' '.substr($row['lname'],0,1).'.)</option>');
}
mysql_close($con);
It will compare if the user in $_GET['to'] is the same as $row['id'] in table, if yes, the $selected will be created. This was for a private messaging system...
它会比较 $_GET['to'] 中的用户是否与表中的 $row['id'] 相同,如果是,则将创建 $selected。这是一个私人消息系统......
回答by Muhammad Fahad
Simple and easy to understand example by using ternary operators to set selected value in php
通过使用三元运算符在 php 中设置选定值的简单易懂的示例
<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $id=> $value) { ?>
<option value="<?php echo $id;?>" <?php echo ($id== '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>
回答by Zaheer Ahmad
Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.
它太旧了,但我也必须添加我的方式 :) 因为它是通用且有用的,尤其是当您使用静态下拉值时。
function selectdCheck($value1,$value2)
{
if ($value1 == $value2)
{
echo 'selected="selected"';
} else
{
echo '';
}
return;
}
and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns
在你的下拉选项中,你可以像这样使用这个功能,你可以尽可能多地使用它,因为它适合你所有的选择框/下拉菜单
<option <?php selectdCheck($row[month],january); ?> value="january">january</option>
:) I hope this function help others
:) 我希望这个功能可以帮助其他人
回答by Sean R
This is the solution that I came up with...
这是我想出的解决方案......
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="select_month">
<?php
if (isset($_POST['select_month'])) {
if($_POST["select_month"] == "January"){
echo '<option value="January" selected="selected">January</option><option value="February">February</option>';
}
elseif($_POST["select_month"] == "February"){
echo '<option value="January">January</option><option value="February" selected="selected">February</option>';
}
}
else{
echo '<option value="January">January</option><option value="February">February</option>';
}
?>
</select>
<input name="submit_button" type="submit" value="Search Month">
</form>
回答by user2142997
Simple way
简单的方法
<select class ="dropdownstyle" name="category" selected="<?php print($messageeditdetails[0]['category_id']); ?>">
<option value=""><?php echo "Select"; ?></option>
<?php foreach ($dropdowndetails as $dropdowndetails) { ?>
<option <?php if($messageeditdetails[0]['category_id'] == $dropdowndetails['id']) { ?> selected="<?php echo $dropdowndetails['id']; ?>" <?php } ?> value="<?php echo $dropdowndetails['id']; ?>"><?php echo $dropdowndetails['category_name']; ?></option>
<?php } ?>
</select>
回答by Sani Kamal
A Simple Solution: It work's for me
一个简单的解决方案:它对我有用
<div class="form-group">
<label for="mcategory">Select Category</label>
<select class="form-control" id="mcategory" name="mcategory" required>
<option value="">Please select category</option>
<?php foreach ($result_cat as $result): ?>
<option value="<?php echo $result['name'];?>"<?php
if($result['name']==$mcategory){
echo 'selected';
} ?>><?php echo $result['name']; ?></option>
}
<?php endforeach; ?>
</select>
</div>
回答by Adrian
Check this:
检查这个:
<select class="form-control" id="department" name="department" type="text">
<option value="medical-furniture" @if($list->department == "medical-furniture") selected @endif>Medical furniture</option>
<option value="medical-table" @if($list->department == "medical-table") selected @endif>Medical table</option>
<option value="service" @if($list->department == "service") selected @endif>Service</option>
</select>
回答by StudioX
My suggestion is to leverage the hidden/collapse attribute. Try with this example:
我的建议是利用隐藏/折叠属性。试试这个例子:
<select>
<option value="echo $row[month]" selected disabled hidden><? echo $row[month] ?></option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
</select>
in case of null for $row[month]the selected item is blank and with data, it would contain less codes for many options and always working for HTML5 and bootstrap etc...
如果$row[month]所选项目的 null 为空白且有数据,它将包含较少的许多选项代码,并且始终适用于 HTML5 和引导程序等...

