如何在同一页面上保留两个不同的选择选项并获得其值

时间:2019-01-30 13:52:34

标签: php

有人可以向我解释如何设置两个选择选项并获得其值。

到目前为止,我有这个:

php

<form method="POST" action="" enctype="multipart/form-data">
<select class="custom-select-sm" id="inputGroupSelect01" aria-label="Small" aria-describedby="inputGroup-sizing-sm" onchange="window.location='?htid='+this.value" name="selHomeTeam">
<option value="-1">Choose...</option>
<?php 
$teams = Team::getAll();
foreach ($teams as $team) {
echo "<option " . ($selectedHomeTeam->team_id == $team->team_id?"selected":"") . " value='{$team->team_id}'>{$team->name}</option>";
}
?>
</select>
</div></td>
<td>
<div class="input-group input-group-sm mb-3">
<div class="input-group-prepend">
</div>
<select class="custom-select-sm" id="inputGroupSelect01" aria-label="Small" aria-describedby="inputGroup-sizing-sm" onchange="window.location='?atid='+this.value" name="selAwayTeam">
<option value="-1">Choose...</option>
<?php 
 $teams = Team::getAll();
foreach ($teams as $team) {
echo "<option " . ($selectedAwayTeam->team_id == $team->team_id?"selected":"") . " value='{$team->team_id}'>{$team->name}</option>";
}
?>
</select>

<?php 

if (isset($_GET['htid'])) {
  $selectedHomeTeam = Team::getById($_GET['htid']);
} else {
  $selectedHomeTeam = new Team;
  $selectedHomeTeam->team_id = null;
}  if (isset($_GET['atid'])) {
  $selectedAwayTeam = Team::getById($_GET['atid']);
} else {
  $selectedAwayTeam = new Team;
 $selectedAwayTeam->team_id = null;
}

如果我单击第一个选择选项,则会得到他的值,但是如果我单击第二个选择选项,则会丢失第一个选项。
我想要的是同时选择两个值并获取它们的值,就像这样:

image

1 个答案:

答案 0 :(得分:0)

提交表单,而不更改窗口位置。 首先将ID添加到表单中

<form method="POST" action="" enctype="multipart/form-data" id="myForm">

然后在更改选择框值时,提交如下表格,

onchange="$('#myForm').submit()"

对于两个选择框。

如果您使用POST作为表单方法,则请求数组中所选下拉列表的值将类似于

$_POST['selAwayTeam']
$_POST['selHomeTeam']