我已经添加到购物车,但是当我单击“添加到购物车”按钮时,我不想重新加载页面,因此即使它没有重新加载页面,我仍然可以选择其他产品。

Index.php

<?php
if(isset($_GET['action']) && $_GET['action']=="add"){
    $id=intval($_GET['id']);
    if(isset($_SESSION['cart'][$id])){
        $_SESSION['cart'][$id]['quantity']++;
    }else{
        $sql_p="SELECT * FROM products WHERE product_id={$id}";
        $query_p=mysqli_query($con, $sql_p);
        if(mysqli_num_rows($query_p)!=0){
            $row_p=mysqli_fetch_array($query_p);
            $_SESSION['cart'][$row_p['product_id']]=array("quantity" => 1, "price" => $row_p['product_price']);
        }else{
            $message="Product ID is invalid";
        }
    }
}
?>


product.php

<?php
if(isset($_GET['action']) && $_GET['action']=="add"){
    $id=intval($_GET['id']);
    if(isset($_SESSION['cart'][$id])){
        $_SESSION['cart'][$id]['quantity']++;
    }else{
        $sql_p="SELECT * FROM products WHERE product_id={$id}";
        $query_p=mysqli_query($con, $sql_p);
        if(mysqli_num_rows($query_p)!=0){
            $row_p=mysqli_fetch_array($query_p);
            $_SESSION['cart'][$row_p['product_id']]=array("quantity" => 1, "price" => $row_p['product_price']);
        }else{
            $message="Product ID is invalid";
        }
    }
}
?>
<h1>Products</h1>
<?php
if(isset($message)){
    echo "<h2>$message</h2>";
}
?>
<table>
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>Price</th>
        <th>Items Price</th>
    </tr>
    <?php
    $query = mysqli_query($con,"SELECT * FROM products WHERE product_category= 'Chicken' ORDER BY product_name ASC");
    while($row=mysqli_fetch_assoc($query)){
    ?>
    <tr>
        <td><?php echo $row['product_name']; ?></td>
        <td><?php echo $row['product_description']; ?></td>
        <td><?php echo "$" . $row['product_price']; ?></td>
        <td><a href="index.php?page=product&action=add&id=<?php echo $row['product_id']; ?>">Add to Cart</a></td>
    </tr>
    <?php
    }
    ?>
</table>


Cart.php

<?php
    if(isset($_POST['submit'])){
        if(!empty($_SESSION['cart'])){
        foreach($_POST['quantity'] as $key => $val){
            if($val==0){
                unset($_SESSION['cart'][$key]);
            }else{
                $_SESSION['cart'][$key]['quantity']=$val;
            }
        }
        }
    }
?>

<h1>View Cart || <a href="index.php?page=product">Products</a></h1>
<form method="post" action="index.php?page=cart">
<table>
    <tr>
        <th>Name</th>
        <th>Quantity</th>
        <th>Price</th>
        <th>Subtotal</th>
    </tr>
    <?php
    $sql = "SELECT * FROM products WHERE product_id IN(";
            foreach($_SESSION['cart'] as $id => $value){
            $sql .=$id. ",";
            }
            $sql=substr($sql,0,-1) . ") ORDER BY product_id ASC";
            $query = mysqli_query($con, $sql);
            $totalprice=0;
            if(!empty($query)){
            while($row = mysqli_fetch_array($query)){
                $subtotal= $_SESSION['cart'][$row['product_id']]['quantity']*$row['product_price'];
                $totalprice += $subtotal;
    ?>
    <tr>
        <td><?php echo $row['product_name']; ?></td>
        <td><input type="text" name="quantity[<?php echo $row['product_id']; ?>]" size="6" value="<?php echo $_SESSION['cart'][$row['product_id']]['quantity']; ?>"> </td>
        <td><?php echo "$" .$row['product_price']; ?></td>
        <td><?php echo "$" .$_SESSION['cart'][$row['product_id']]['quantity']*$row['product_price']. ".00"; ?></td>
    </tr>

    <?php
            }
            }else{
    ?>
            <tr><td colspan="4"><?php echo "<i>Add product to your cart."; ?></td></tr>
    <?php
            }
    ?>
    <tr>
        <td colspan="3">Total Price: <h1><?php echo "$" ."$totalprice". ".00"; ?></h1><td>
    </tr>
</table>
<br/><button type="submit" name="submit">Update Cart</button>
</form>
<br/><p>To remove an item, set quantity to 0.</p>

最佳答案

   //how to solve this please all kind guys can direct change code, my error is undifined offset value of product_id
 <?php
    include('dataconn.php');
        $result=mysql_query("select * from product");


    while($row=mysql_fetch_assoc($result))
            {
                echo "<p>$row[Product_Name]</p>";

                    echo "<p>$row[Product_Quantity]</p>";
                    echo "<p><a href=\"payment.php?action=add&pid=$row[Product_ID]&qty=1\">Add To Cart</a></p>";






            }
    ?>
    <a href="logout2.php">logout</a>

    /*--add to cart page--*/
    <?php
    /* ----------- ADD TO CART PAGE ----------------*/
    include("dataconn.php");
    $product_id = isset($_GET['pid']) ? $_GET['pid'] : null;
    $action = isset($_GET['action']) ? $_GET['action'] : null; //the action
    $quantity=isset($_GET['qty']) ? $_GET['qty'] : null;
    ?>

    <html>
    <head>
        <title>View Cart</title>


    </head>
    <body>

    <?php
    $customer_id=$_SESSION['customer_id'];
    $result=mysql_query("select * from product");
    $row=mysql_fetch_assoc($result);
    switch($action)
    {
    //decide what to do
    case "add":
        $_SESSION['cart'][$product_id]++;


    break;
    case "remove":
        $_SESSION['cart'][$product_id] = 0;
        unset($_SESSION['cart'][$product_id]);
        header("Location: payment.php");//if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
    break;
    case "empty":
        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
        header("Location: payment.php");
    break;
    }
    //------- for checkout ---------
    if(isset($_SESSION['cart']) && $_SESSION['cart']) {
        $total = 0;
        echo "<form name='cart' method='POST' action=''>";
        echo "<table border=\"1\" padding=\"3\" width=\"100%\" class=\"data-container\">";
         echo "<td colspan=\"4\" align=\"right\"><a href=\"payment.php?action=empty\" onclick=\"return confirm('Are you sure?');\"><img src=\"image/delete.png\"/></a></td>";

        foreach($_SESSION['cart'] as $product_id => $quantity)
        {
            $sql = sprintf("SELECT * FROM product WHERE Product_ID = %d;", $product_id);
            $result = mysql_query($sql);
            if(mysql_num_rows($result) > 0)
            {
                $itemsInfo = mysql_fetch_assoc($result);
                $cost = $itemsInfo["Product_Price"] * $quantity;
                $total = $total + $cost; //add to the total cost
                echo "<tr>";

                //show this information in table cells
                echo "<td align=\"center\">".$itemsInfo["Product_Name"]."</td>";
                //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
                echo "<td align=\"center\">$quantity </td>";
                echo "<td align=\"center\">RM $cost</td>";
                 echo "<td align=\"center\"><a href=\"payment.php?action=remove&pid=$product_id\"><img src=\"image/remove.png\"/></a></td>";
                echo "</tr>";
            }
        }
        //show the total
        echo "<tr>";
        echo "<td colspan=\"2\" ></td>";
        echo "<td align=\"center\">Total</td>";
        echo "<td colspan=\"2\" align=\"center\">RM $total</td>";
        echo "</tr>";
        echo "</table>";
        echo "</form>";
        }

        ?>
        <br>
        <div>
        <span style="margin-left: 88%"><a href="product3.php"><input type="submit" name="shopping_more"  class="custom-button" value="Back To Shopping"/></a></span>
        </div>


        </html>

10-08 00:23