Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

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

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.

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...