2013-02-26 119 views
2

如何在請求後仍然顯示已篩選的選擇。提交後保持選擇可見

所以,如果我有選擇1,2和3。當我選擇2和數據表明我仍然希望2顯示錶示該數據是通過選項2.

echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >"; 
echo "<select name='Country' tabindex='1' >"; 

while($row = mysql_fetch_array($result)) 
{ 
    echo " <option value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>"; 
} 
echo "</select>"; 
echo "<input type='submit' value='Filter' />"; 
echo "</form>"; 
+0

你需要檢查接線柱陣列,其值及改變html相應 – 2013-02-26 20:36:42

回答

2

過濾,你需要的是選定的屬性添加到選項:

$Country = $_POST['Country']; 
$sected = 'selected = "selected" '; 
while($row = mysql_fetch_array($result)) 
{ 
    echo " <option ".($row['Fund_Manager_Company_Code'] == $Country? $selected : '')."value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>"; 
} 

的價值選擇,並張貼然後將選擇的一個...

3

你可以做這樣的事情:

$country = isset($_POST['Country']) ? $_POST['Country'] : ''; 
    while($row = mysql_fetch_array($result)) 
    { 
     echo " <option value='". $row['Fund_Manager_Company_Code'] ."' ".(($row['Fund_Manager_Company_Code'] == $country) ? 'selected="selected"' : '').">". $row['Fund_Manager_Company_Name'] ."</option>"; 
    } 
+1

您需要檢查$ _POST ['Country']是否存在,因爲將會發出通知 – Winston 2013-02-26 20:39:05

0

echo" <option value='" . $row['Fund_Manager_Company_Code'] . "' " . ((isset($_POST['Country']) && $_POST['Country'] == $row['Fund_Manager_Company_Code']) 
? 'selected="selected"' : '') . ">" . $row['Fund_Manager_Company_Name'] . "</option>"; 
0

後您的提交,你需要捕捉選擇在你的PHP代碼:

$selection = $_POST['Country']; 

echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >"; 
echo "<select name='Country' tabindex='1' >"; 

while($row = mysql_fetch_array($result)) 
{ 
    $selected = ""; 
    if ($row['Fund_Manager_Company_Code'] == $selection) { 
    $selected = "selected"; 
    } 
    echo " <option value='". $row['Fund_Manager_Company_Code'] ."' ".$selected.">". $row['Fund_Manager_Company_Name'] ."</option>"; 
} 
echo "</select>"; 
echo "<input type='submit' value='Filter' />"; 
echo "</form>";