我正在建立一个网站,我是php新手。当你尝试将baterang添加到购物车时,它只是添加了一个baterang,但是当你添加一个tripleX时,它会增加一个三层和baterang。请帮助。
这是我的主页
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="icon" href="favicon.ico"/>
<script src="jquery.js"></script>
<script>
</script>
</head>
<body>
<main class="container">
<?php
$Items = $_GET["newItems"];
$cart = explode("+",$Items)
?>
<div>
<div class="nav">
<img id="logo" width="40" src="/images/logo-main.png"/> Spinners3d.com
| <a id="home" href="http://spinners3d.com" style="text-
decoration:none; color:white;" >Home</a> | <a id="help"
href="http://spinners3d.com/help.html" style="text-decoration:none;
color:white;" >Support</a> | <a id="cart"
href="http://spinners3d.com/cart.php" style="text-decoration:none;
color:white;" >Cart</a>
<br>
</div>
<div class="spinners">
Spinners
<br>
<br>
<img width="60" src="/images/hillary.jpg"/>
<br>
Baterang
<br>
<a href="http://spinners3d.com/cart.php?newItem=<?php array_push($cart,
'Baterang'); echo $tran = implode('+', $cart);?>" style="text-
decoration:none; color:#4fa7ff;" >Add To Cart</a>
<br>
<br>
<img width="60" src="/images/hillary.jpg">
<br>
TripleX
<br>
<a href="http://spinners3d.com/cart.php?newItem=<?php array_push($cart,
'TripleX'); echo $tran = implode('+', $cart);?>" style="text-
decoration:none; color:#4fa7ff;" >Add To Cart</a>
<br>
<br>
<img width="60" src="/images/hillary.jpg"/>
<br>
The Dragon
<br>
<a href="http://spinners3d.com/cart.php?newItem=<?php array_push($cart,
'TheDragon'); echo $tran = implode('+', $cart);?>" style="text-
decoration:none; color:#4fa7ff;" >Add To Cart</a>
<pre>
<?php print_r($cart); ?>
</pre>
</div>
</main>
</body>
</html>
这是购物车页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="icon" href="favicon.ico"/>
<script src="jquery.js"></script>
<script>
</script>
</head>
<body>
<?php $newItems = $_GET[newItem];?>
<?php $cart = explode("+",$newItems);?>
<div class="nav">
<img id="logo" width="40" src="/images/logo-main.png"/>
Spinners3d.com |
<a id="home" href="http://spinners3d.com?newItems=<?php echo $tran =
implode('+', $cart); ?>" style="text-decoration:none; color:white;"
>Home</a> | <a id="help" href="http://spinners3d.com/help.html"
style="text-decoration:none; color:white;" >Support</a> | <a
id="cart"
href="http://spinners3d.com/cart.php" style="text-decoration:none;
color:white;" >Cart</a>
</div>
<?php foreach($cart as $item);
echo '<div id=spinners>'.$item.'<div/>';
?>
</body>
</html>
提前致谢
答案 0 :(得分:0)
这是有道理的:
<a href="http://spinners3d.com/cart.php?newItem=<?php array_push($cart,
'Baterang'); echo $tran = implode('+', $cart);?>" style="text-
decoration:none; color:#4fa7ff;" >Add To Cart</a>
将Baterang项添加到$cart
。在实际关注链接之前,请不要将内容推送到$cart
阵列。现在,它在显示页面时将每个项目添加到购物车。
您的链接可能只是?newItem=Baterang
这在您的购物车页面中:
<?php
$newItem = $_GET['newItem'];
array_push($cart, $newItem);
?>
我认为您正在混合客户端和服务器端逻辑。单击<a>
标记时没有完成推送,就像JS中的情况一样,当加载cart.php
时,这一切都发生在服务器端。