且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在表单提交后仍能选择下拉菜单值?

更新时间:2022-12-02 17:10:27

这是我会做的。它不是富有魅力或完美,但它的工作非常好。如果表单提交的值匹配,您只需设置一个变量即可将选定的属性添加到选项标签中。然后只有当该变量与该选项匹配时才输出所选属性。这可能更难,但是这样做很简单。

Here is what I would do. It isn't glamourous or perfect, but it works amazingly well. You simply set a variable to add the selected attribute to your option tags IF the value coming from the form submit matches. Then output the selected attribute only if that variable matches that option. This can be way harder, but this works and is simple to understand.

为选择本身添加一个值对你来说并不会做任何事情。它必须添加到要自动选择的特定选项上。

Adding a value to the select itself doesn't do anything for you really. It has to be added on the particular option that you want to be auto-selected.

<?php if($orderfield=='ordersessionid'){$session = ' selected="selected"';} ?>
<?php if($orderfield=='ordermoduleid'){$module = ' selected="selected"';} ?>
<?php if($orderfield=='orderteacherid'){$teacher = ' selected="selected"';} ?>
<?php if($orderfield=='orderstudentid'){$student = ' selected="selected"';} ?>
<?php if($orderfield=='ordergrade'){$grade = ' selected="selected"';} ?>

<select name="order">
    <option value="ordersessionid" <?php echo $session; ?>>Session ID</option>
    <option value="ordermoduleid" <?php echo $module; ?>>Module Number</option>
    <option value="orderteacherid" <?php echo $teacher; ?>>Teacher Username</option>
    <option value="orderstudentid" <?php echo $student; ?>>Student Username</option>
    <option value="ordergrade" <?php echo $grade; ?>>Grade</option>
 </select>