nav100
nav100

Reputation: 3133

keep the selected value for dropdown after submit

How do I keep the selected item after I submit the page? I have the country dropdown list with the following code.

  <?php 
                    $SQL = "SELECT countr_id, country_name FROM countries"; 
                    $Result = mysql_query($SQL) or die(mysql_error());
                ?> 
                 <select name="country" style="width:400px">    <option value='-1'></option>        
                <?php 
                    while($row = mysql_fetch_array($Result))
                    {       
                        echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";        
                    }  
                ?>  

Upvotes: 2

Views: 16351

Answers (7)

kathy
kathy

Reputation: 171

//assume you have $result = array(your result list);
<select name='question'>
<?php
foreach ($result as $question) {
    if ($_POST['question'] == $question) {
        $selected = "selected";
    } else {
        $selected = '';
    }
    echo "<option value='" . $question . "' $selected>$question</option>";
}
?>
</select>

Upvotes: 0

Laurence Burke
Laurence Burke

Reputation: 2358

This code depends if you are trying to keep the value after you submit back to the original page.

<?php 
     $SQL = "SELECT countr_id, country_name FROM countries"; 
     $Result = mysql_query($SQL) or die(mysql_error());
?> 
   <select name="country" style="width:400px">    
      <option value='-1'></option>        
<?php 
   while($row = mysql_fetch_array($Result))
   {       
      echo "<option ";
      if($_REQUEST["yourSelectName"] ==$row["country_id"])
         echo ' selected = "selected" ';
         echo " value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";        
   }  
?>  
   </select>

Upvotes: 1

Marcx
Marcx

Reputation: 6826

  <?php 
       $selectedid = 5; //example of selected if before submitting
       $SQL = "SELECT countr_id, country_name FROM countries"; 
       $Result = mysql_query($SQL) or die(mysql_error());
  ?> 
     <select name="country" style="width:400px">    <option value='-1'></option>        
  <?php 
     while($row = mysql_fetch_array($Result))
    {       
     echo "<option value=\"".$row["country_id"]." ".(($selected==$row["country_id"])?"SELECTED":"")."\">".$row["country_name"]."</option>";        
    }  
  ?>  

Upvotes: 0

Austin Best
Austin Best

Reputation: 1058

Or you can do it all inline... less code, doesn't require all the if/then/else stuff

Change

echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";

To

echo "<option ". (($_POST['country'] == $row["country_id"]) ? 'selected ' : '') ."value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";

If it is get (passed in URL), the use GET

Upvotes: 0

mrkmg
mrkmg

Reputation: 241

<?php 
    $SQL = "SELECT countr_id, country_name FROM countries"; 
    $Result = mysql_query($SQL) or die(mysql_error());
?> 
<select name="country" style="width:400px">    <option value='-1'></option>        
<?php 
    while($row = mysql_fetch_array($Result))
    {       
        echo "<option ";
        if($_REQUEST['country'] == $row["country_id"]) echo 'selected="selected" ';
        echo "value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";        
    }  
?> 

Upvotes: 1

David Chan
David Chan

Reputation: 7505

           <?php 
                while($row = mysql_fetch_array($Result))
                {   
                    if ($_POST['country'] == $row["country_id"]) {

                        echo "<option value=\"".$row["country_id"]."\" selected="selected">".$row["country_name"]."</option>"; 
                    } else {
                        echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";       
                    }
                }  
            ?>  

Upvotes: 1

Sgn.
Sgn.

Reputation: 1696

  <?php 
        $SQL = "SELECT countr_id, country_name FROM countries"; 
        $Result = mysql_query($SQL) or die(mysql_error());
  ?> 
  <select name="country" style="width:400px">    <option value='-1'></option>        
  <?php 
        while($row = mysql_fetch_array($Result))
        {       
              echo "<option value=\"".$row["country_id"]."\"";
              if($_POST['country'] == $row['country_id'])
                    echo 'selected';
              echo ">".$row["country_name"]."</option>";        
        }  
  ?>  

Upvotes: 2

Related Questions