<form action='' method="POST">
<select class="form-control" id='qty' name='qty'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
<input type="submit" name="submit" value="Add to cart">
</form>
<?php
if (isset($_POST['submit']))
{
$qty = $_POST['qty'];
echo "$qty";
}
?>
我正在尝试从我的select标签中获取其中一个选项。但我不知道如何使用表单从post方法中获取它。它必须显示通过表单获得的数据。如果我在编写php时出错,请纠正我
答案 0 :(得分:1)
您必须使用按钮提交表单或在更改数量时提交表单。
平变化= “this.form.submit()”
<form action='' method="POST">
<select class="form-control" id='qty' name='qty' onchange="this.form.submit()">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
</form>
<?php
if (isset($_POST['qty'])){
$qty = $_POST['qty'];
echo $qty;
}
?>
如果javascript被禁用,则上述代码将无效,在这种情况下您必须使用提交按钮
<form action='' method="POST">
<select class="form-control" id='qty' name='qty'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST['qty'])){
$qty = $_POST['qty'];
echo $qty;
}
?>