如何在PHP MySQL或javascript中增加/删除产品数量?或者如何将产品的数量值与复选框一起插入?
<form action="addtocart.php" method="post">
<table width="100%" cellspacing="20px">
<?php
include 'dbconnect.php';//for connection
$sql='select * from product';
$res = $dbconn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$res->execute();
$disp="<tr><th>Image</th><th>Product Name</th><th>Price</th><th>Add To Cart</th></tr>";
while($rs=$res->fetch())
{
if($rs['availableOnline']==1)
$isonline="Yes";
else
$isonline="No";
if($rs['availableInStore']==1)
$isstore="Yes";
else
$isstore="No";
$disp.="<tr>";
$disp.="<td><img src=".$rs['imageLink']." width=50 height=70></img></td>";
$disp.="<td>".$rs['name']."</td>";
$disp.="<td>$ ".$rs['price']."</td>";
$disp.="<td><input type=checkbox value=".$rs['idProduct']." name=items[] > Add Item</td></tr>";
$disp.="<tr><td> </td><td>Available Online : ".$isonline."</td><td>Available in Store :".$isstore."</td><td> </td></tr>";
}
echo $disp;
?>
<tr><td> </td><td><input type="submit" id="btnsubmit" value="Add To Cart" /></td></tr>
</table>
</form>
让我用一个例子告诉你,对于动态数量变化,我们可以使用javascript
index . php
<a herf="#" onclick="increase()"> Increse</a>
<input type="number" min=0 max=100 id="quantity" name="quantity" value="1">
<a herf="#" onclick="decrease()"> Decrease</a>
<input type="checkbox" name="fruit[]" value="1">Orange<br>
<input type="checkbox" name="fruit[]" value="2">Banna<br>
<input type="checkbox" name="fruit[]" value="3">Kiwi<br>
javscript我们将使用'id'来获取数字(输入)的值
function increase()
{
e=parseFloat(document.getElementById("quantity").value);
e+=1;
document.getElementById('quantity').value=e;
}
function decrease(){
e=document.getElementById("quantity").value;
e-=1;
//to prevent from quantity going less than zero
if(e>0)
{
document.getElementById('quantity').value=e;
}
}
save.php在第二个PHP中,我们将使用内爆函数来存储多个值
$quantity= $_POST['quantity'];
$fruit= implode(',' , $_POST['fruits']);
echo $fruit; //used that value to store in database result will be like this 1,2,5,6