简体   繁体   English

根据组合框选择显示结果

[英]displaying results based on combobox selection

I have a dynamic combobox and I have my Fetch button. 我有一个动态组合框,并且有我的提取按钮。 When a user selects a value from combobox and clicks fetch button, all the other related values are displayed in a textbox for the user to edit and update records. 当用户从组合框中选择一个值并单击提取按钮时,所有其他相关值将显示在文本框中,供用户编辑和更新记录。 And that works fine. 那很好。

<form id="form1" method="post" action="edit.php">   
  <select name="ID" id="select">
     <?php display_Id();?>
  </select> 
  <input type="submit" name="Fetch" id="Fetch" value="Fetch" />
</form>

function display_Id() {
    $query = "SELECT * FROM Flight";

    $result = mysql_query($query) or die("Failed to fetch records");
    confirm_query($result);

    while($rows = mysql_fetch_array($result)) {
        $flightNum = $rows['FlightNo'];
        echo "<option value=\"$flightNum\" ";
        echo " selected";
        echo "> $flightNum </option>";
    }       
}

The problem is in the Fetch button. 问题出在“获取”按钮中。 When user clicks Fetch, other values are displaying but the selected value from combobox is refreshing. 当用户单击“获取”时,将显示其他值,但是从组合框中选择的值正在刷新。 How to make the values remain selected even after pressing the Fetch button? 即使按下“获取”按钮后,如何使这些值保持选定状态?

Your question is incomplete in the sense, that you don't have your dislay_Id() code shown here. 从某种意义上说,您的问题是不完整的,因为这里没有显示dislay_Id()代码。 However, Generally speaking, you should add selected after <option value="something" programmatically, 但是,通常来说,您应该以编程方式在<option value="something"之后添加selected

Code should be something like this: 代码应该是这样的:

function displayId(){
  if($value[x]== $currentValue) {
    echo "<option value='$value[x]' selected>sth</option>";
  } 
  else
  {
    echo "<option value='$value[x]'>sth</option>";
  }
}

EDIT:: Your code adds a "selected" to each of the values, you must only add a "selected" to a current value. 编辑::您的代码向每个值添加一个“选定的”,您必须仅向当前值添加一个“选定的”。

So, your code must look like this: 因此,您的代码必须如下所示:

echo "<option value=\"$flightNum\" ";

if($_POST['ID'] == $flightNum)
{        
  echo " selected";
}
echo "> $flightNum </option>";
while($rows = mysql_fetch_array($result))
{
    $flightNum = $rows['FlightNo'];
    echo "<option value=\"$flightNum\" ";
    if($_POST['ID'] == $flightNum)
    {
        echo " selected";
    }
    echo "> $flightNum </option>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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