8

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!

1

8 Answers 8

14

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

<option selected="selected" value="available">Available</option>
Sign up to request clarification or add additional context in comments.

Comments

3

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?

1 Comment

Pro Tip: there is absolutely no advantage/benefit in repeating the option's text as the value declaration.
1
<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.

1 Comment

It is basic php code editing. I could type it out for you, but you really should figure it out your self. TIP : Use separate echo commands for your fields/commands.
1
<?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>

Comments

1

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>

Comments

1

You can set the selected dropdown option from database as below:

<select name="status"> 
<option <?php echo ($row['status'] == 'Available') ? 'selected' : '' ?> value='Available'>Available</option> 
<option <?php echo ($row['status'] == 'Not Available') ? 'selected' : '' ?> value='Not Available'>Not Available</option> 
</select>

1 Comment

Pro Tip: there is absolutely no advantage/benefit in repeating the option's text as the value declaration. "You can do this" is not an explanation of how your snippet works or why it is a good idea.
0

Declare the options in an array like

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

Comments

0

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

<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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.