javascript - 我怎样才能在这个购物车php中插入数量

标签 javascript php jquery html mysql

我已成功将商品添加到购物车。但是,我无法添加它的数量。我知道问题出在 addtocart 中的某个地方,我无法将索引中的值传递到下一页。我对此仍然是初学者,所以我不知道如何解决这个问题。我尝试过使用 session ,但仍然不起作用

这是我在index.php中的代码:

<html>
    <head>
        <meta charset="UTF-8">
        <title>Innisnotfree</title>
    </head>
    <body>
        <?php
            session_start();
           
            include'header.php';
            include'nav.php';
            include "connection.php";
            
            $sql = "SELECT * FROM products";
            $res = mysqli_query($connection, $sql);
        ?>
        
        <div class="container">
            
            <div class="row">
               
                <?php while($r = mysqli_fetch_assoc($res)){ ?>
                <div class="col-sm-6 col-md-3">
                    <div class="thumbnail">
                        <img src="<?php echo $r['image'];?>" alt="<?php echo $r['title']?>">
                        <div class="caption">
                            <form name="form1" id="form1" method="POST" action="addtocart.php?id=<?php echo $r['id']?>">
                                <h3><?php echo $r['title']?></h3>
                                <p> <?php echo $r['description']?></p>
                                <p><?php echo $r['price']?></p>
                                <p><input type="number" name="quantity" id="quantity" class="form-control"/></p>
                                <p><input type="submit" name="add_to_cart" id="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart"/></p>
                            </form>
                        </div>
                    </div>
                </div>
                <?php } ?>
            </div>
        </div>
        
        <?php include('footer.php')?>
 
    </body>
</html>

这是我的 addtocart.php

<?php
    session_start();
    
if(isset($_SESSION['cart']) & !empty($_SESSION['cart'])){
	$items = $_SESSION['cart'];
	$cartitems = explode(",", $items);
	$items .= "," . $_GET['id'];
        if($_SERVER["REQUEST_METHOD"]=="POST"){
        $quantity = $_POST["quantity"];}
	header('location: index.php?status=success');
}else{
	$items = $_GET['id'];
	$_SESSION['cart'] = $items;
         if($_SERVER["REQUEST_METHOD"]=="POST"){
        $_POST["quantity"] = $quantity ;}
	header('location: index.php?status=success');
          
}
 ?>

这是我的 cart.php:

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
            session_start();
            require_once('connection.php');
            include('header.php');
            include('nav.php');
            
            $items = $_SESSION['cart'];
            if($_SERVER["REQUEST_METHOD"]=="POST"){
                $quantity = $_POST["quantity"];
            }
            $cartitems = explode(",", $items);
            
       
        ?>
        
         
        <div class="container">
            <div class="row">
                <table class="table">
                    <tr>
                        <th>S.NO</th>
                        <th>Item Name</th>
                        <th>Quantity</th>
                        <th>Price </th>
                    </tr>
                    <?php
                        $total = 0;
                        $i =1;
                        
                        foreach ($cartitems as $key=>$id){
                            $sql = "SELECT * FROM products WHERE id = $id";
                            $res=  mysqli_query($connection, $sql);
                            if(empty($_SESSION['cart'])){
                                $r = 0;
                                
                            }
                            else{
                                 $r = mysqli_fetch_assoc($res);
      
                            }
                        ?>
                   
                    <tr>
                        <td><?php echo $i; ?></td>
                        <td><a href="delcart.php?remove=<?php echo $key; ?>">Remove</a><?php echo $r['title'];?></td>
                        <td><?php  if($_SERVER["REQUEST_METHOD"]=="POST"){echo $_POST["quantity"];};?></td>
                        <td>$<?php echo $r['price'];?></td>
                    </tr>
                    <?php
                    
                        $total = $total +  $r['price'];
                        $i++;
                        }
                    ?>
                    <tr>
                        <td><strong>Total Price</strong></td>
                        <td><strong>$<?php echo $total; ?></strong></td>
                        <td><a href="#" class="btn btn-info">Checkout</a></td>
                    </tr>
                </table>
            </div>
        </div>
        <?php 
            include 'footer.php';
        ?>
        
       
    </body>
</html>

最佳答案

查看我的购物车类,您可以根据需要进行修改

class cart {
/**
 * add product to cart by id and quantity
 * @param type $pid product id
 * @param type $quantity 
 */
function add($pid,$quantity){

/*if cart dosen't exist initialize it and add the first product*/
    if(!isset($_SESSION['cart'])){
        $_SESSION['cart']=array();
        $_SESSION['cart'][0]['productid']=$pid;
        $_SESSION['cart'][0]['quantity']=$quantity;
        echo 'Product successfully added';
    }
/*else if cart exist check if product exist to change quantity else add new product*/
    else {
/*if exist change quantity*/
        if($this->isexist($pid,$quantity)){
            echo 'Quantity updated successfully';
        }
/*else add new product in cart*/
        else{
            $m=$_SESSION['cart'];
            $max=count($m);
            $_SESSION['cart'][$max]['productid']=$pid;
            $_SESSION['cart'][$max]['quantity']=$quantity;
            echo 'Product successfully added';
        }
    }

}

/**
 * check if product alredy exist in cart or not
 * @param type $pid product id
 * @param type $quantity
 * @return boolean if true the product is exist and the quantity change successfully
 */
function isexist($pid,$quantity) {
    $m=$_SESSION['cart'];
    $max=count($m);
    for($i=0;$i<$max;$i++){
        if($pid==$_SESSION['cart'][$i]['productid']){
            $_SESSION['cart'][$i]['quantity']=$quantity;
            return true;
        }
    }
    return false;

}

/**
 * delete product by product id
 * @param type $pid product id
 */
function delete($pid){
    $m=$_SESSION['cart'];
    $max=count($m);
    for($i=0;$i<$max;$i++){
        if($pid==$_SESSION['cart'][$i]['productid']){
        unset($_SESSION['cart'][$i]);
        $_SESSION['cart']=array_values($_SESSION['cart']);$_SESSION['cart'.'num']-=1;break;
        }
    }
}

/**
 * change quantity of exist product
 * @param type $pid product id
 * @param type $quantity
 */
function modify($pid,$quantity){
    $m=$_SESSION['cart'];
    $max=count($m);

    if($quantity>0){
        for($i=0;$i<$max;$i++){
            if($pid==$_SESSION['cart'][$i]['productid']){
                $_SESSION['cart'][$i]['quantity']=$quantity;break;
            }
        }
    }

    else {$this->delete($pid);}

}

/**
 * show all items in your cart
 */
function show_cart() {

    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        echo 'id=>'.$_SESSION['cart'][$i]['productid'].
             'quantity=>'.$_SESSION['cart'][$i]['quantity'].'<br>';
    }
}

}

关于javascript - 我怎样才能在这个购物车php中插入数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47842589/

相关文章:

javascript - GreenSock,补间函数参数

javascript - 如何获取从用户那里获取数字的正则表达式

jQuery:鼠标移动事件 - 元素内向左、向右

javascript - 如何在使用 highcharter 制作的箱线图中添加平均线?

javascript - 在 Canvas 中绘制二叉树时节点重叠

javascript - 通过这两种不同的方法调用 jQuery 功能的优缺点是什么?

php - 如何在三列中显示 wordpress 帖子?

javascript - 替换后字符提交错误

php - 将链接转换成图片

php - 如何在 Windows 8 的 xmpp 中安装 zend