0

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?

<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 を使用できます。

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>

javascript 「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 2 番目の 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 に答える