【问题标题】:How to pass data from current page to another page?如何将数据从当前页面传递到另一个页面?
【发布时间】:2020-10-19 09:10:34
【问题描述】:

我在我的 PHP 文件中创建了一个简单的购物车,我为此使用了会话,我能够完美地显示订单点击并显示在 cart.ph 中。现在,我为购物车页面中客户订购的每件商品添加了一个数量文本字段。现在我想要摘要页面,其中包含客户在他/她中输入的产品名称和数量,但显然我只能在购物车中获得一个数量正确的产品,其余的默认为 1 这是我的代码

这是我的购物车.ph

<?php
session_start();
$status="";
if (isset($_POST['action']) && $_POST['action']=="remove"){
if(!empty($_SESSION["shopping_cart"])) {
    foreach($_SESSION["shopping_cart"] as $key => $value) {
        if($_POST["code"] == $key){
        unset($_SESSION["shopping_cart"][$key]);
        $status = "<div class='box' style='color:red;'>
        Product is removed from your cart!</div>";
        }
        if(empty($_SESSION["shopping_cart"]))
        unset($_SESSION["shopping_cart"]);
            }       
        }
}

if (isset($_POST['action']) && $_POST['action']=="change"){
  foreach($_SESSION["shopping_cart"] as &$value){
    if($value['code'] === $_POST["code"]){
        $value['quantity'] = $_POST["quantity"];
        break; // Stop the loop after we've found the product
    }
}
    
}
?>
<html>
<head>
<title>SKIP-CART</title>
<link rel='stylesheet' href='css/style.css' type='text/css' media='all' />
</head>
<body>
<div style="width:700px; margin:50 auto;">

<h2>Shopping Cart</h2>   

<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
?>
<div class="cart_div">
<a href="cart.php">
<img src="cart-icon.png" /> Cart
<span><?php echo $cart_count; ?></span></a>
</div>
<?php
}
?>

<div class="cart">
<?php
if(isset($_SESSION["shopping_cart"])){
    $total_price = 0;
?>  
<table class="table">
<tbody>
<tr>
<td></td>
<td>ITEM NAME</td>
<td>QUANTITY</td>
</tr>   
<?php       
foreach ($_SESSION["shopping_cart"] as $product){
?>
<tr>
<td><img src='<?php echo $product["image"]; ?>' width="50" height="40" /></td>
<td><?php echo $product["name"]; ?><br />
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />
<input type='hidden' name='action' value="remove" />

<button type='submit' class='remove'>Remove Item</button>
</form>
</td>
<td>
<form method='post' action='display.php' id='orderform'>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />
<input type='hidden' name='action' value="change" /> 
<input type="text" id="quantity" name="quantity">                                                                                                                                                                                                 
</select>
</form>
</td>
<td></td>
<td></td>
</tr>
<?php

}
?>
<tr>
<td colspan="5" align="right">

<strong><button type="submit" form="orderform" value="submit">Proceed Order</button></strong>
</td>
</tr>
</tbody>
</table>        
  <?php
}else{
    echo "<h3>Your cart is empty!</h3>";
    }
?>
</div>

<div style="clear:both;"></div>

<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>



<br/><br />

</div>
</body>
</html>

这是我的 display.php 此文件将获取购物车中的所有产品和客户/用户编辑的数量,但显然我只能根据 cart.php 显示 1 个正确的数量记录

<?php

session_start();
$status="";
if (isset($_POST['action']) && $_POST['action']=="remove"){
if(!empty($_SESSION["shopping_cart"])) {
    foreach($_SESSION["shopping_cart"] as $key => $value) {
        if($_POST["code"] == $key){
        unset($_SESSION["shopping_cart"][$key]);
        $status = "<div class='box' style='color:red;'>
        Product is removed from your cart!</div>";
        }
        if(empty($_SESSION["shopping_cart"]))
        unset($_SESSION["shopping_cart"]);
            }       
        }
}

?>
<html>
<head>
<title>SKIP-CART</title>
<link rel='stylesheet' href='css/style.css' type='text/css' media='all' />
</head>
<body>
<div style="width:700px; margin:50 auto;">

<h2>Shopping Cart</h2>   

<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
?>
<div class="cart_div">
<a href="cart.php">
<img src="cart-icon.png" /> Cart
<span><?php echo $cart_count; ?></span></a>
</div>
<?php
}
?>

<div class="cart">
<?php
if(isset($_SESSION["shopping_cart"])){
    $total_price = 0;
?>  
<table class="table">
<tbody>
<tr>
<td></td>

</tr>   
<?php       
foreach ($_SESSION["shopping_cart"] as $product){
?>
<tr>
<td><img src='<?php echo $product["image"]; ?>' width="50" height="40" /></td>
<td><?php echo $product["name"]; ?><br />
</td>
<td><?php echo $product["quantity"]; ?><br />
</td>
<td></td>
<td></td>
</tr>
<?php

}
?>
<tr>
<td colspan="5" align="right">

<strong><button type="submit" form="orderform" value="submit">Proceed Order</button></strong>
</td>
</tr>
</tbody>
</table>        
  <?php
}else{
    echo "<h3>Your cart is empty!</h3>";
    }
?>
</div>

<div style="clear:both;"></div>

<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>



<br/><br />

</div>
</body>
</html>

这是购物车的示例图片:

当我点击继续订单时,这是我的 display.php

【问题讨论】:

  • 您的“Proceed Order”提交按钮似乎不属于任何这些表单的一部分,所以我看不出您将如何获取甚至 one的数据> 提交的产品...?
  • 我从我创建的名为 shopping_cart 的会话中获取数据,因此我可以显示我创建的会话中的数据,但我无法在 cart.php 的摘要页面基础中显示数量我只能显示1 个正确数量

标签: php html arrays forms


【解决方案1】:

我建议您将所有这些都保存在数据库中。

创建carts 表并在其中添加您需要的所有字段,并在id 之后的开头创建字符串uuid

为每个访问您网站的客户创建一个uuid 并将其放入cookies 中,当将用户添加到购物篮时,使用此uuid 在数据库中创建一个条目。 uuid 将是用户 ID。如果您有登录名,请使用user_id。然后通过uuiduser_id(如果有用户登录)从数据库中输出。

显示数据库中的记录比填写服务器会话更方便

【讨论】:

  • 在数据库中创建购物车?一个非常糟糕的主意!这可能会杀死数据库,因为数据文件会越来越大,最后你会用完空间,删除这些记录不会解决问题;有会话,数据库不应该用作临时存储
  • 购物篮应始终与订单状态一起存储在数据库中。未来会有在线支付、订单统计等数据使用cart_id附加到订单表中。你当然决定。但是数据库是最好的选择。
  • 但不是在它制作之前,在订购屏幕之间
  • @FlashThunder 是正确的,所以我为此使用会话。显然我无法从用户输入的数量的 cart.php 中获得正确的数据
猜你喜欢
  • 1970-01-01
  • 2020-11-20
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
相关资源
最近更新 更多