0

如何在 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>&nbsp;</td><td>Available Online : ".$isonline."</td><td>Available in Store :".$isstore."</td><td>&nbsp;</td></tr>";
            }
            echo $disp;
        ?>
          <tr><td>&nbsp;</td><td><input type="submit"  id="btnsubmit"  value="Add To Cart"  /></td></tr>
    </table>
</form>
4

1 回答 1

0

让我举个例子,对于动态数量变化,我们可以使用javascript

索引.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 中我们将使用 implode 函数来存储多个值

$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
于 2013-04-21T02:39:08.330 回答