doukang7858 2013-07-30 08:22 采纳率: 0%
浏览 31

将另一页上的下拉列表的默认值设置为等于上一页上选择的值

I have the following code that generates a dropdown list. I want the value selected to be the default value of the same drop-down list on another page. The code I am using to generate the drop-down list is area_index.php:

// Write out our query.
    $query = "SELECT * FROM hub";
    // Execute it, or return the error message if there's a problem.
    $result = mysql_query($query) or die(mysql_error());

    $dropdown = "<select name='hub'>";
    while($row = mysql_fetch_assoc($result)) {

    $dropdown .= "
<option value='{$row['name']}'>{$row['name']}</option>";
    }
    $dropdown .= "
</select>";

?>
<form method="post" align= "right">
<table >
    <tr>
        <td>Select the Hub for which you want to add and area: </td>
        <td><?php echo $dropdown; ?></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="submit" value="Add" align= "right" /></td>
    </tr>

</table>

I want to display the same drop-down list in my page called area_view.php but the default value of the drop-down list must be equal to the selected value.

The variable in area_index.php used is $hub_name = $_POST['hub'];

  • 写回答

1条回答 默认 最新

  • doupuxuan5784 2013-07-30 08:26
    关注

    Change your while loop to this:

    while($row = mysql_fetch_assoc($result)) {
        $dropdown .= "
    <option value='{$row['name']}'" . ($row['name'] == $_POST['hub'] ? "selected" : "") . ">{$row['name']}</option>";
    }
    

    This checks to see if the value you're passing as $_POST['hub'] from the previous page equals the value of $row['name'] for each option being added. For the one that's equal, it will add the attribute selected, which will cause the browser to show that option as the selected one.

    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失