<选择>下拉默认值

2021-12-28 00:00:00 default-value drop-down-menu php html

我有这个代码:

if(isset($_POST['search']))
{
    $res1=mysql_query("SELECT * FROM aircraft where acode = '$_POST[ac]'") or die(mysql_error());
    while($row=mysql_fetch_array($res1))
    {
        $airc=$row['acode'];
        $amode=$row['amodel'];
        $stat=$row['status'];
        $rem=$row['remarks'];

    echo "<center><table><form name="frmMain" method="post"> 
        <tr><td><font face=consolas><b>Aircraft Code:</b></font></td><td><input type=text name=arc value='$airc' readonly=readonly></td></tr>
        <tr><td><font face=consolas><b>Aircraft Model:*</b></font></td><td><input type=text name=am value='$amode'></td></tr>
        <tr><td><font face=consolas><b>Status:*</b></font></td><td><input type=text name=st value='$stat'></td></tr>
        <tr><td><font face=consolas><b>Remarks:*</b></font></td><td><input type=text name=rm value='$rem'></td></tr></table>";
    }
}

在提交搜索"按钮时,此代码显示飞机表中的数据.允许用户使用 (*) 符号更新数据.

On submit 'search' button, this code displays the data from aircraft table. The user is allowed to update the data with the (*) sign.

由于状态默认如下(可用,不可用),我改了这个

Since the Status are the following by default (Available, Not Available), I changed this

 <tr><td><font face=consolas><b>Status:*</b></font></td><td><input type=text name=st value='$stat'></td></tr>

至此,

<tr><td><font face=consolas><b>Status:*</b></font></td><td><select name=st>
    <option value=Available>Available</option>
    <option value='Not Available'>Not Available</option>
</select></td></tr>

但我希望下拉菜单的默认值取决于$stat=$row['status']; 因为这是一个更新表单.

But I want the dropdown to have it's default value depending on $stat=$row['status']; since this is an update form.

如果正在检索的数据的状态为可用",则下拉列表的默认值应为可用".

If the data being retrieved has it's status 'Available', then the dropdown should have it's default value as 'Available'.

我怎样才能做到这一点?我试过 <select name=status value='$stat'> 但它不起作用.任何帮助将不胜感激.谢谢!

How can I achieve that? I tried <select name=status value='$stat'> but it doesn't work. Any help will be appreciated. Thanks!

推荐答案

只需将 selected="selected" 放在选项上,具体取决于您的 $row['status'],

Just put selected="selected" on the option depending on your $row['status'],

<option selected="selected" value="available">Available</option>

相关文章