php 如何用php预先选择一个html下拉列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3479130/
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 preSelect an html dropdown list with php?
提问by artur
I am trying to get the option selected using PHP, but I ran out of ideas!
我正在尝试使用 PHP 选择该选项,但我没有任何想法!
Below is the code I have tried until now:
以下是我迄今为止尝试过的代码:
<select>
<option value="1">Yes</options>
<option value="2">No</options>
<option value="3">Fine</options>
</select>
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
So, what do I have to do?
那么,我该怎么办?
回答by Christian
Programmers are lazy...er....efficient....I'd do it like so:
程序员很懒惰......呃......高效......我会这样做:
<select><?php
$the_key = 1; // or whatever you want
foreach(array(
1 => 'Yes',
2 => 'No',
3 => 'Fine',
) as $key => $val){
?><option value="<?php echo $key; ?>"<?php
if($key==$the_key)echo ' selected="selected"';
?>><?php echo $val; ?></option><?php
}
?></select>
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
回答by Bart
<select>
<option value="1" <?php if ($myVar==1) echo 'selected="selected"';?>>Yes</options>
<option value="2" <?php if ($myVar==2) echo 'selected="selected"';?>>No</options>
<option value="3" <?php if ($myVar==3) echo 'selected="selected"';?>>Fine</options>
</select>
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
This is a very simple and straightforward way, if I understand your question correctly.
如果我正确理解您的问题,这是一种非常简单直接的方法。
回答by bernte
you can use this..
你可以用这个..
<select name="select_name">
<option value="1"<?php echo(isset($_POST['select_name'])&&($_POST['select_name']=='1')?' selected="selected"':'');?>>Yes</option>
<option value="2"<?php echo(isset($_POST['select_name'])&&($_POST['select_name']=='2')?' selected="selected"':'');?>>No</option>
<option value="3"<?php echo(isset($_POST['select_name'])&&($_POST['select_name']=='3')?' selected="selected"':'');?>>Fine</option>
</select>
回答by Iznogood
First of all give a name to your select. Then do:
首先为您的选择命名。然后做:
<select name="my_select">
<option value="1" <?= ($_POST['my_select'] == "1")? "selected":"";?>>Yes</options>
<option value="2" <?= ($_POST['my_select'] == "2")? "selected":"";?>>No</options>
<option value="3" <?= ($_POST['my_select'] == "3")? "selected":"";?>>Fine</options>
</select>
What that does is check if what was selected is the same for each and when its found echo "selected".
这样做是检查每个选择的内容是否相同以及何时找到的回声“选择”。
回答by Maarten de Graaf
I use inline if's
我使用内联如果
($_POST['category'] == $data['id'] ? 'selected="selected"' : false)
回答by marouska
I have 2 php files and i made this, and it works. (this is an example) the first code is from the one file and the second code from two file.
我有 2 个 php 文件,我做了这个,它有效。(这是一个例子)第一个代码来自一个文件,第二个代码来自两个文件。
<form action="two.php" method="post">
<input type="submit" class="button" value="submit" name="one"/>
<select name="numbers">
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
</select>
</form>
if(isset ($_POST['one']))
{
if($_POST['numbers']=='1')
{
$a='1' ;
}
else if($_POST['numbers']=='2')
{
$a='2' ;
{
else if ($_POST['numbers']=='3')
{
$a='3' ;
}
}
回答by didierc
I suppose that you are using an array to create your select
form input.
In that case, use an array:
我想您正在使用数组来创建select
表单输入。在这种情况下,请使用数组:
<?php
$selected = array( $_REQUEST['yesnofine'] => 'selected="selected"' );
$fields = array(1 => 'Yes', 2 => 'No', 3 => 'Fine');
?>
<select name=‘yesnofine'>
<?php foreach ($fields as $k => $v): ?>
<option value="<?php echo $k;?>" <?php @print($selected[$k]);?>><?php echo $v;?></options>
<?php endforeach; ?>
</select>
If not, you may just unroll the above loop, and still use an array.
如果没有,您可以只展开上述循环,并且仍然使用数组。
<option value="1" <?php @print($selected[$k]);?>>Yes</options>
<option value="2" <?php @print($selected[$k]);?>>No</options>
<option value="3" <?php @print($selected[$k]);?>>Fine</options>
Notes that I don't know:
我不知道的注意事项:
- how you are naming your input, so I made up a name for it.
- which way you are handling your form input on server side, I used
$_REQUEST
,
- 你如何命名你的输入,所以我为它起了一个名字。
- 您在服务器端处理表单输入的方式,我使用了
$_REQUEST
,
You will have to adapt the code to match requirements of the framework you are using, if any.
您必须调整代码以匹配您正在使用的框架的要求(如果有)。
Also, it is customary in many frameworks to use the alternative syntaxin view dedicated scripts.
此外,在许多框架中习惯于在视图专用脚本中使用替代语法。
回答by Vince
This is the solution that I've came up with:
这是我想出的解决方案:
<form name = "form1" id = "form1" action = "#" method = "post">
<select name = "DropDownList1" id = "DropDownList1">
<?php
$arr = array('Yes', 'No', 'Fine' ); // create array so looping is easier
for( $i = 1; $i <= 3; $i++ ) // loop starts at first value and ends at last value
{
$selected = ''; // keep selected at nothing
if( isset( $_POST['go'] ) ) // check if form was submitted
{
if( $_POST['DropDownList1'] == $i ) // if the value of the dropdownlist is equal to the looped variable
{
$selected = 'selected = "selected"'; // if is equal, set selected = "selected"
}
}
// note: if value is not equal, selected stays defaulted to nothing as explained earlier
echo '<option value = "' . $i . '"' . $selected . '>' . $arr[$i] . '</option>'; // echo the option element to the page using the $selected variable
}
?>
</select> <!-- finish the form in html -->
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
</form>
The code I have works as long as the values are integers in some numeric order ( ascending or descending ). What it does is starts the dropdownlist in html, and adds each option element in php code. It will not work if you have random values though, i.e: 1, 4, 2, 7, 6. Each value must be unique.
只要值是按某种数字顺序(升序或降序)的整数,我的代码就可以工作。它所做的是在 html 中启动下拉列表,并在 php 代码中添加每个选项元素。但是,如果您有随机值(即:1、4、2、7、6),它将不起作用。每个值必须是唯一的。
回答by Tom Lime
This answer is not relevant for particular recepient, but maybe useful for others. I had similiar issue with 'selecting' right 'option' by value returned from database. I solved it by adding additional tag with applied display:none.
此答案与特定收件人无关,但可能对其他人有用。我在通过数据库返回的值“选择”正确的“选项”时遇到了类似的问题。我通过添加带有应用显示的附加标签解决了这个问题:无。
<?php
$status = "NOT_ON_LIST";
$text = "<html>
<head>
</head>
<body>
<select id=\"statuses\">
<option value=\"status\" selected=\"selected\" style=\"display:none\">$status</option>
<option value=\"status\">OK</option>
<option value=\"status\">DOWN</option>
<option value=\"status\">UNKNOWN</option>
</select>
</body>
</html>";
print $text;
?>