如何使用php preSelect html下拉列表? [英] How to preSelect an html dropdown list with php?

查看:133
本文介绍了如何使用php preSelect html下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面是我迄今为止所尝试的代码:

 < select> 
< option value =1>是< / options>
< option value =2>否< / options>
< option value =3> Fine< / options>
< / select>
< input type =textvalue =name =name>
< input type =submitvalue =goname =go>

那么我该怎么办?

解决方案

程序员懒惰...呃....高效...我会这样做:



< pre class =lang-php prettyprint-override> < 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 =textvalue =name =name>
< input type =submitvalue =goname =go>


I am trying to get the option selected using PHP, but I ran out of ideas!

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?

解决方案

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">

这篇关于如何使用php preSelect html下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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