0

我正在使用 PHP/mySQL 构建一个测试购物网站

这是我的代码:

show_product.php:

<form method ="get" action="addtocart.php">
                <input type="hidden" name="id_product" value='.$row['id_product'].'>
                    <tr>
                        <th colspan="2"><a href="#">'.$row['name'].'</a></th>
                    </tr>                     
                        <td>
                        <a href="#"><img align="center"  src='.$row['photo'].' alt=""></a>
                    </td>
                    <td>'.mb_substr($row['description'],0,200).'....</td>                                                 
                    <tr></tr>                           
                        <td>
                        '.$row['price'].'€&lt;input type="number" name="quantity" value="1" min="1" max="20">
                    </td>

                    <td> <button type="submit" class="btn">add to cart</button></form>

addtocart.php

session_start()
if(isset($_GET) & !empty($_GET)){   
    $id_product = $_GET['id_product'];
    if(isset($_GET['quantity']) & !empty($_GET['quantity'])){ $quant = $_GET['quantity']; }else{ $quant = 1;}
    $_SESSION['cart'][$id_product] = array("quantity" => $quant);   
    header('location: cart.php');
}else{

    header('location: cart.php');
}
echo "<pre>";
print_r($_SESSION['cart']);
echo "</pre>";

?>

和 cart.php 的一些代码

    foreach ($cart as $key => $value) {
                    $cartsql = "SELECT * FROM product WHERE id_product=$key";
                    $cartres = mysqli_query($con, $cartsql);
                    $row = mysqli_fetch_assoc($cartres);    
             ?>
                <tr><form>
                   <td colspan="2"><?php echo $row['name'] ?></td>
                    <td><?php echo $row['price'].' €'; ?></td>
                   **<td><input type="number" class="quantity" name="quantity" value="<?php echo $value['quantity']; ?>"></td>**         
                    <td><?php echo ($row['price']*$value['quantity']).' €'; ?> </td>
                    <td><a href="delcart.php?id_product=<?php echo $key; ?>"><img width="30" height="30" src="icons/delete.png" alt=""></a></td> </tr>

如果我向 addcart 发送一定数量的物品,它就可以工作。

问题是我无法更改cart.php 里面的数量,我对Javascript 知之甚少。请任何帮助如何做到这一点?

4

1 回答 1

0

我是通过购物车内的提交表单完成的:)

<form class="userform" method='get' name="change" action="addtocart.php">
                        <input type='hidden' name='id_product' value="<?php echo $row["id_product"]; ?>" />
                        <td><input size="5" type="number" name="quantity" value="<?php echo $value['quantity'] ?>" min="1" max="20" onchange="this.form.submit()"></td></form>
于 2020-03-12T16:12:36.263 回答