如何使用 php 预先选择一个 html 下拉列表?

2021-12-28 00:00:00 return drop-down-menu select php option

我正在尝试使用 PHP 选择该选项,但我没有任何想法!

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

那我该怎么办?

推荐答案

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

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

相关文章