提交后保持选择可见

时间:2013-02-26 20:32:38

标签: php html mysql drop-down-menu

如何在请求后仍然显示已过滤的选择。

所以,如果我有选项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>";

4 个答案:

答案 0 :(得分: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 :(得分: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>";
}

将选择并随后发布的值...

答案 2 :(得分: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>";

答案 3 :(得分: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>";