首页
  • 问答
  • 教程
  • 书籍
  • IT新闻
  • <select> dropdown default value

    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.

    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.

    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!


    根据您的$row['status'] ,只需在选项上放入selected="selected"

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

    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?


    <?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>
    
    链接地址: http://www.djcxy.com/p/88084.html

    上一篇: 如何在多重选择中将“所有值”设置为默认值?

    下一篇: <select>下拉默认值