2011-10-20 193 views
2

我在提交頁面後如何保留所選項目?我有以下代碼的國家下拉列表。提交後保留所選下拉值

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

OMG lol ANSWER OVERLOAD –

+0

什麼是您的select元素的名稱,您是否正在張貼或獲取您的方法? –

回答

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

此代碼依賴,如果你想保留值提交回原來的頁面之後。

<?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> 
0

或者你也可以做到這一切內嵌...更少的代碼,並不需要所有的if/then/else語句的東西

變化

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

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

如果得到(通過URL),使用GET

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