如何使用 php 预选 html 下拉列表?

2022-01-19 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">

相关文章