简体   繁体   中英

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>";        
                    }  
                ?>  
  <?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>";        
        }  
  ?>  
           <?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>";       
                    }
                }  
            ?>  
<?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>";        
    }  
?> 

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>
//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>

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

  <?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>";        
    }  
  ?>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM