<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>
<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>
<?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代码。这段代码实际上显示了一个可以选择的列表,然后是创建和编辑两个按钮。
我希望这些按钮重定向到另一个php脚本,但是,如果单击编辑,我需要将选择列表的值添加到url中。我现在用的方法不起作用,有人知道我是怎么做到的吗?
发布于 2018-06-26 03:59:51
请检查以下代码。您可以使用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>
<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....
}
?>
https://stackoverflow.com/questions/51029175
复制相似问题