PHP 如何保持下拉列表中选定的选项在提交时保持选中状态?

发布于 2024-10-08 12:50:34 字数 863 浏览 0 评论 0原文

我有:

<select name="topic" style="margin-bottom:3px;"> 
    <option>General Question</option>
    <option>Company Information</option>
    <option>Customer Issue</option>
    <option>Supplier Issue</option>
    <option>Request For Quote</option>
    <option>Other</option>
</select>

用于下拉。当提交表单时,它会进入验证页面。如果有错误,表单会保留用户输入的原始内容。我让它适用于所有输入字段和文本区域,但我如何通过下拉菜单来做到这一点?

我通过使用以下方式保留输入字段:

$name = $_REQUEST["name"];

在再次显示的表单中,有(忽略它在表格中的事实):

<tr>
    <td>Name:*</td>
     </tr>
     <tr>
    <td><input name="name" type="text" size="15" value="<?php echo $name ?>" maxlength="200" /></td>
     </tr>

那么,对于下拉菜单有什么想法吗?

I have:

<select name="topic" style="margin-bottom:3px;"> 
    <option>General Question</option>
    <option>Company Information</option>
    <option>Customer Issue</option>
    <option>Supplier Issue</option>
    <option>Request For Quote</option>
    <option>Other</option>
</select>

for the drop down. And when the form is submitted, It goes to a validation page. If it has errors the form keeps the original content the user put in. I have it working for all of the input fields and textarea's, but how could I do this with a drop down?

I have the input fields staying by using:

$name = $_REQUEST["name"];

and in the form that shows up again, there is (ignore the fact that it is in a table):

<tr>
    <td>Name:*</td>
     </tr>
     <tr>
    <td><input name="name" type="text" size="15" value="<?php echo $name ?>" maxlength="200" /></td>
     </tr>

So, any ideas for drop downs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2024-10-15 12:50:34

您需要将“selected”属性添加到适当的选项。我相信您还需要指定每个选项的值属性。我不确切知道你是如何生成该列表的,但这也许会有所帮助:

<?php
$options = array( 1=>'General Question', 'Company Information', 'Customer Issue', 'Supplier Issue', 'Supplier Issue', 'Request For Quote', 'Other' );
$topic = $_REQUEST['topic']; // the topic name would now be $options[$topic]

// other PHP etc...
?>

<select name="topic" style="margin-bottom:3px;"> 
    <?php foreach ( $options as $i=>$opt ) : ?>
        <option value="<?php echo $i?>" <?php echo $i == $topic ? 'selected' : ''?>><?php echo $opt ?></option>
    <?php endforeach; ?>
</select>

You need to add the "selected" attribute to the appropriate option. I believe you also need to specify the value attribute for each option. I don't know exactly how you are generating that list, but maybe this will help:

<?php
$options = array( 1=>'General Question', 'Company Information', 'Customer Issue', 'Supplier Issue', 'Supplier Issue', 'Request For Quote', 'Other' );
$topic = $_REQUEST['topic']; // the topic name would now be $options[$topic]

// other PHP etc...
?>

<select name="topic" style="margin-bottom:3px;"> 
    <?php foreach ( $options as $i=>$opt ) : ?>
        <option value="<?php echo $i?>" <?php echo $i == $topic ? 'selected' : ''?>><?php echo $opt ?></option>
    <?php endforeach; ?>
</select>
苍风燃霜 2024-10-15 12:50:34

首先,为选项元素赋予一个 value 属性。这使得代码更加健壮,因为如果您决定更改选项的文本,它不会中断。在那之后:

<?php $topic = $_REQUEST['topic']; ?>
<?php $attr = 'selected="selected"'; ?>
<select name="topic" style="margin-bottom:3px;"> 
    <option value="1" <?php echo $topic == 1 ? $attr : ''; ?>>General Question</option>
    <option value="2" <?php echo $topic == 2 ? $attr : ''; ?>>Company Information</option>
    <option value="3" <?php echo $topic == 3 ? $attr : ''; ?>>Customer Issue</option>
    <option value="4" <?php echo $topic == 4 ? $attr : ''; ?>>Supplier Issue</option>
    <option value="5" <?php echo $topic == 5 ? $attr : ''; ?>>Request For Quote</option>
    <option value="6" <?php echo $topic == 6 ? $attr : ''; ?>>Other</option>
</select>

First of all, give the option element a value attribute. This makes the code more robust, because it does not break should you decide to alter the text of an option. After that:

<?php $topic = $_REQUEST['topic']; ?>
<?php $attr = 'selected="selected"'; ?>
<select name="topic" style="margin-bottom:3px;"> 
    <option value="1" <?php echo $topic == 1 ? $attr : ''; ?>>General Question</option>
    <option value="2" <?php echo $topic == 2 ? $attr : ''; ?>>Company Information</option>
    <option value="3" <?php echo $topic == 3 ? $attr : ''; ?>>Customer Issue</option>
    <option value="4" <?php echo $topic == 4 ? $attr : ''; ?>>Supplier Issue</option>
    <option value="5" <?php echo $topic == 5 ? $attr : ''; ?>>Request For Quote</option>
    <option value="6" <?php echo $topic == 6 ? $attr : ''; ?>>Other</option>
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文