【问题标题】:How to access value of select list in html using php如何使用 php 在 html 中访问选择列表的值
【发布时间】:2018-06-26 03:59:51
【问题描述】:

<form action="pickup_locations.php" method="post">
            <div class="row">
                <div class="col-md-4">
                    <!--List Select-->
                    <select class="form-control" size="10" name="locationId" onselect="selectPickupLocation()">
                        <?php
                        $locations = getPickupLocationList();
                        foreach($locations as $location) {
                            echo '<option value="'.$location['id'].'">'.$location['name'].'</option>';
                        }
                        ?>
                    </select>
                </div>
            </div>
            &nbsp;
            <div class="row">
                <div class="col-md-12">
                    <div class="btn-group" role="group">
                        <!--Create and Edit Buttons-->
                        <p><a href="pickup_location_form.php?action=create" class="btn btn-secondary">Create</a></p>
                        &nbsp;
                        <?php
                        $locationId = $_POST['locationId'];
                        echo '<p><a href="pickup_location_form.php?action=edit&locationId='.$locationId.'" class="btn btn-secondary">Edit</a></p>';
                        ?>
                    </div>
                </div>
            </div>
        </form>

以上是我的html代码。此代码实质上显示了一个可以选择的列表,然后是 2 个按钮,创建和编辑。

我希望这些按钮重定向到另一个 php 脚本,但是,如果单击编辑,我需要将选择列表的值添加到 url。我现在的方法不行,有谁知道我该怎么做?

【问题讨论】:

  • 只有在您第一次将表单提交到 PHP 后才会出现。在以这种方式提交之前,您无法获取 POST 值。您可能想使用 javascript 来获取选项值中的内容,并将其附加到 href,然后重定向用户。
  • 是否创建和编辑线索 POST 到同一个脚本?如果他们确实发布到同一个脚本,您可以检查记录是否存在,如果不是创建,是否进行编辑。如果他们没有发布到同一个脚本,您可以使用 javascript 更改点击时要执行的 url。

标签: php html post request href


【解决方案1】:

请检查以下代码。 您可以使用 POST 数据在pickup_location_form.php 加载编辑表单 将编辑链接转换为提交按钮和表单的 action="pickup_location_form.php?action=edit"

<form action="pickup_location_form.php?action=edit" method="post">
    <div class="row">
        <div class="col-md-4">
            <select class="form-control" size="10" name="locationId" onselect="selectPickupLocation()">
                <?php
                $locations = getPickupLocationList();
                foreach($locations as $location) {
                    echo '<option value="'.$location['id'].'">'.$location['name'].'</option>';
                }
                ?>
                </select>
        </div>
    </div>
    &nbsp;
    <div class="row">
        <div class="col-md-12">
            <div class="btn-group" role="group">
                <p><a href="pickup_location_form.php?action=create" class="btn btn-secondary">Create</a></p>
                <p><button class="btn btn-secondary">Edit</button></p>
            </div>
        </div>
    </div>
</form>

在pickup_location_form.php 文件中:

<?php
//pickup_location_form.php
$action = isset($_GET['action'])? $_GET['action'] : '';
if ($action == 'create') {
    // ... Load the Edit form here ...
}
else if (($action == 'edit') && !empty($_POST['locationId'])) {
    $locationId = $_POST['locationId'];
    // ... Load the Edit form here for the $locationId ...
}
else {
    // Neither Create or Edit was selected, you redirect to the previous page here or do something else....
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2017-11-11
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多