[英]how to increase/delete quantity of a product in PHP MySQL or javascript. Or how to insert a quantity value of a product along with a checkbox
How to increase/delete quantity of a product in PHP MySQL or javascript? 如何增加/删除PHP MySQL或javascript产品的数量? Or how to insert a quantity value of a product along with a checkbox?
或如何插入产品的数量值和复选框?
<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>
Let me show you with an example, for dynamic quantity change , we can use javascript 让我为您展示一个示例,对于动态数量变化,我们可以使用javascript
index.php 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 We will used the 'id' to get the value of a number(input) 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 in second php we will use implode function to store multiple value 在第二个php中的save.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.