php <select> 下拉默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12293939/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:16:26  来源:igfitidea点击:

<select> dropdown default value

phphtmldrop-down-menudefault-value

提问by xjshiya

I have this code:

我有这个代码:

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.

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

enter image description here

在此处输入图片说明

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>

to this,

对此,

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

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

但我希望下拉菜单具有默认值, $stat=$row['status'];因为这是一个更新表单。

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

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

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

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

回答by Vinay

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

只需selected="selected"根据您的情况选择选项$row['status']

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

回答by Sam Janssens

write Available and Unavailable into an array

将可用和不可用写入数组

$theArray = array("Available","Not Available");

loop the array:

循环数组:

<tr><td><font face=consolas><b>Status:*</b></font></td><td><select name=st>
<?php
foreach ($theArray as $key => $value) {
    if ($value == $stat) {
        echo('<option selected="selected" value='.$value.'>'.$value.'</option>');
    } else {
        echo('<option value='.$value.'>'.$value.'</option>');
    }
}
?>
</select></td></tr>

and in each loop we check if the value in the array, is the same as it is in the variable, if so, we put the selected there

在每个循环中,我们检查数组中的值是否与变量中的值相同,如果是,我们将选定的值放在那里

understand the logic?

明白逻辑吗?

回答by A.Aleem11

You can define your variable value with additional option tag and mark that as selected like:

您可以使用附加选项标签定义变量值,并将其标记为已选择,例如:

<select name="role" id="role">
<!-- This is default define value using php variable $r -->
<option selected="selected" value="<?php echo $r; ?>" disabled="disabled"><?php echo $r; ?></option>

<!-- Other options values -->
<option value="2">Option-2</option><option value="2">Option-2</option>
</select>

回答by Pragnesh Chauhan

you can do this using below code:

您可以使用以下代码执行此操作:

<?php
$stat=$row['status'];
$selected_a = ($stat == 'Available') ? 'selected' : '' ;
$selected_b = ($stat == 'Not Available') ? 'selected' : '' ;
?>
<select name=st> 
<option <?php echo $selected_a; ?> value='Available'>Available</option> 
<option <?php echo $selected_b; ?> value='Not Available'>Not Available</option> 
</select>

回答by amitchhajer

<?php
$status = "navail";
?>
<select name="sel">
<option value="avail" <?php if($status == "avail") echo "SELECTED";?> > Avail </option>
<option value="navail" <?php if($status == "navail") echo "SELECTED";?> > Navail </option>
</select>

回答by Rohan Sood

<select name=status>
<option value="available" <?php if($row['status']=="available") echo "selected=\"selected\""; ?>>Available</option>
<option value="unavailable" <?php if($row['status']=="unavailable") echo "selected=\"selected\""; ?>>Unvailable</option>
</select>

Basically echo selected="selected" for the option depending on value of the concerned field.

基本上 echo selected="selected" 取决于相关字段的值。

回答by justin

its my first time here though and i tried using basic principles in php, dont know if this helps you but if you're trying to grab the defaulted value which you inputted on your database then edit it again i suggest you try this one

这是我第一次来这里,我尝试在 php 中使用基本原则,不知道这是否对您有帮助,但是如果您试图获取您在数据库中输入的默认值,然后再次编辑它,我建议您尝试这个

<select name="st">
<?php if($row['status']=="Available"){?><option value="Available">Available</option><?php }?>
<?php if($row['status']=="Unavailable"){?><option value="Unavailable">Unavailable</option><?php }?>
</select>

Assuming that the column name in your database is 'status', give it a try works for me

假设您的数据库中的列名是“状态”,试一试对我有用

回答by jeswin

Declare the options in an array like

在数组中声明选项,如

$arr = array("available" => "available","unavailable" => "unavailable");

$arr = array("available" => "available","unavailable" => "unavailable");

and input the drop down like this

并像这样输入下拉菜单

echo form_dropdown("st", $arr, set_value("st", (isset($row['status'];) ? $row['status']; : ""))

This is the method commonly used in frameworks like codeigniter.. i think it works in core php too..

这是 codeigniter 等框架中常用的方法..我认为它也适用于核心 php..