我想使用一个复选框来选择一个行表的项目,但我有问题,以获得每个项目的数量,用户输入从这里是我的html代码。我想用php有人可以帮助我吗?
<form id="RegisterUserForm" action="register.php" method="post">
<table id="t01">
<tr>
<th width="5%">Select</th>
<th width="20%">Picture</th>
<th width="55%">Description</th>
<th width="10%">Amount</th>
<th width="10%">Price</th>
</tr>
<tr>
<td><input id="pedido1" name="check_list[]" type="checkbox" value="1"/>Product 1</td>
<td id="imagem1"><img src="imagen1.png"></td>
<td>Descriptiondo Product 1</td>
<td><input id="qt" name="qt" type="text" class="text" value="1" /></td>
<td>13.50</td>
</tr>
<tr>
<td><input id="pedido1" name="check_list[]" type="checkbox" value="1"/>Product 2</td>
<td id="imagem1"><img src="imagen2.png"></td>
<td>Descriptiondo Product 2</td>
<td><input id="qt" name="qt" type="text" class="text" value="1" /></td>
<td>13.50</td>
</tr>
<tr>
<td><input id="pedido1" name="check_list[]" type="checkbox" value="1"/>Product 3</td>
<td id="imagem1"><img src="imagen3.png"></td>
<td>Descriptiondo Product 3</td>
<td><input id="qt" name="qt" type="text" class="text" value="1" /></td>
<td>13.50</td>
</tr>
<tr>
<td><input id="pedido1" name="check_list[]" type="checkbox" value="1"/>Product 4</td>
<td id="imagem1"><img src="imagen4.png"></td>
<td>Descriptiondo Product 4</td>
<td><input id="qt" name="qt" type="text" class="text" value="1" /></td>
<td>13.50</td>
</tr>
</table>
<button id="register" type="submit">register</button>
</form>
只是一个想法,但是为什么在相同的形式中有一个复选框和金额呢?我的意思是,如果所有的金额都以0(或空白)开头,而用户更改为另一个数字,很明显他想要那个产品。
那么您只能在表单中使用这个参数(每个参数都有特定的名称,可能包含产品的id)。比如:
<form>
//rest of html
<input name="qt[idproduct]" type="text" class="text" value="" />
//rest of html
<input name="qt[idproduct]" type="text" class="text" value="" />
//rest of html
<input name="qt[idproduct]" type="text" class="text" value="" />
//rest of html
</form>
这个表单的输入将产生一个数组参数qt
,您可以通过以下方式获取值:
<?php
if(isset($_GET['qt'])) { //or $_POST
foreach ($_GET['qt'] as $id => $total) {
if(!empty($total) && $total > 0 && is_numeric($total)) { // just checking if you are receiving a number bigger than 0
echo "Product: " . $id . " total: " . $total . "<br />"; // do your thing
}
}
}
?>
与checkboxes
,形成:
<form>
//rest of html
<input name="check_list[idproduct1]" type="checkbox" value="1"/>Product 1
<input name="qt[idproduct1]" type="text" class="text" value="" />
//rest of html
<input name="check_list[idproduct2]" type="checkbox" value="1"/>Product 2
<input name="qt[idproduct2]" type="text" class="text" value="" />
//rest of html
<input name="check_list[idproduct3]" type="checkbox" value="1"/>Product 3
<input name="qt[idproduct3]" type="text" class="text" value="" />
//rest of html
<input type="submit">
</form>
php: <?php
if(isset($_GET['check_list'])) { //or $_POST
foreach ($_GET['check_list'] as $id => $value) {
$total = $_GET['qt'][$id];
if(!empty($total) && $total > 0 && is_numeric($total)) { // just checking if you are receiving a number bigger than 0
echo "Product: " . $id . " total: " . $total . "<br />"; // do your thing
}
}
}
?>