Jump to content

[SOLVED] <select> - remember selected option


osiris1603

Recommended Posts

Hi, I have this so that the select drop down menu gives the option/value of the "status" so it is selected in the drop down menu, so if the status was "Processing" then the first option shown in the <select> would be "Processing" instead of the pre-defined first option "Pending". Although, I am not sure why this is not giving the correct output, as it still stays at "Pending"...

 

Does anyone know how I can resolve this?

 

<?php 
$status = "SELECT status FROM orders WHERE id = $id";  
       $orderstat = mysql_query ($status);  
while ($get_info = mysql_fetch_row($orderstat)){ 
foreach ($get_info as $orderstatus)  
print "$orderstatus"; 
} 

$options = array ("Pending", "Processing", "Completed", "Dispatched", "Not Cleared", "Cancelled");  

$status      = mysql_query ("SELECT status FROM orders WHERE id = ". $id);   
$get_info    = mysql_fetch_row($orderstat);  
$orderstatus = $get_info[0];  

$form = '<select name="status">'."\n";  

foreach ( $options as $option ) {  
    $selected = ( $orderstatus == $option ) ? ' selected="selected"' : '';  
    $form .= '<option value="'. $option .'"'. $selected .'>'. $option .'</option>'."\n";  
}  

$form .= '</select>'; 

print $form; 
?> 

 

Many thanks in advance...

I simply use the following:

 

echo '<select name="myselect">
    <option value="myvalue"';
if($orderstatus == $option) echo ' selected="selected"';
echo '>My Option</option>
</select>';

 

Thank you for your help, although I changed the value of the get_info

 

<?php 
$status = "SELECT status FROM orders WHERE id = $id";  
       $orderstat = mysql_query ($status);  
while ($get_info = mysql_fetch_row($orderstat)){ 
foreach ($get_info as $orderstatus)  
print "$orderstatus"; 
} 

$options = array ("Pending", "Processing", "Completed", "Dispatched", "Not Cleared", "Cancelled");  

$status      = mysql_query ("SELECT status FROM orders WHERE id = ". $id);   
$get_info = mysql_fetch_row($status); // changed this line
$orderstatus = $get_info[0];  

$form = '<select name="status">'."\n";  

foreach ( $options as $option ) {  
    $selected = ( $orderstatus == $option ) ? ' selected="selected"' : '';  
    $form .= '<option value="'. $option .'"'. $selected .'>'. $option .'</option>'."\n";  
}  

$form .= '</select>'; 

print $form; 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.