【问题标题】:Passing an input value to a select option to another page using POST使用 POST 将输入值传递给另一个页面的选择选项
【发布时间】:2020-07-22 08:23:23
【问题描述】:

我有一个来自form1.php 的输入文本,我想将它传递给具有选择选项的form2.php,并且我试图将来自form1.php 的输入文本作为form2 中的选定选项。

尝试将选择值传递给输入时,它看起来很直接,但是当我扭转局面时,我被卡住了。

form1.php:

<form class="form1" action="form2.php" method="POST">
<input type="text" name="inputval">
<button type="submit" name="submit">Submit</button>
</form>

form2.php:

<?php
if (isset($_POST["submit"]))
{
<select name="productselect" id="productselect">
<option>Car</option>
<option>Bus</option>
<option>Plane</option>
<option>Sail Boat</option>
</select>
}
else{}
?>

如果能帮助我指出正确的方向,我们将不胜感激。谢谢。

【问题讨论】:

  • 在form2中,查看$_POST['inputval']的值是,并用它来标记选择的选项。如果您阅读了 html option 标记的语法,您将了解如何决定哪个是预选的。由于它是一个硬编码列表,因此它会比来自数据库或数组的代码多一些,因此您可以将选项移动到数组并循环遍历它们。那么您只需要做一项检查,而不是每行一项。

标签: php html forms post


【解决方案1】:

如果$_POST['inputVal']的值与选项相同,则需要检查每个选择选项,如果是,则添加selected="selected"以将其标记为已选择选项。

还要考虑到如果没有选项被标记为选中,第一个将被自动选中。

<?php

if (isset($_POST["submit"])) {
    ?>
        <select name="productselect" id="productselect" >
            <option <?php if($_POST['inputVal'] == 'Car') echo 'selected="selected"' ?>> Car</option >
            <option <?php if($_POST['inputVal'] == 'Bus') echo 'selected="selected"' ?>> Bus</option >
            <option <?php if($_POST['inputVal'] == 'Plane') echo 'selected="selected"' ?>> Plane</option >
            <option <?php if($_POST['inputVal'] == 'Sail Boat') echo 'selected="selected"' ?>> Sail Boat </option >
        </select >
    <?php
} else {
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-06
    • 2016-08-22
    • 1970-01-01
    • 2011-08-11
    • 2016-07-18
    • 2018-10-01
    • 2011-07-20
    • 2013-05-22
    相关资源
    最近更新 更多