我創建了一個購物車按鈕,並在我的購物車頁面中顯示了一個總計數量,我將其回顯出來。我希望echo'd值位於我的購物車按鈕中,但是當我試圖將回顯添加到該值時,它不起作用。是否有另一種方式來構建這個來獲得我的url圖像,價值:購物車和顯示的echo'd數量?在窗體輸入中回顯php充當路徑按鈕
我的購物車按鈕:
<form class="shopcart" id="block right" action="shoppingcart.php">
<input type="submit" class="shopcart" name="shopcart" value="Shopping Cart <?php echo $totalquantity; ?> ">
</form>
總量計數:
<?php
}
//Shopping Cart Quantity Count
if(isset($_SESSION['shopping_cart']) && is_array($_SESSION['shopping_cart'])) {
$totalquantity = 0;
foreach($_SESSION['shopping_cart'] AS $product) {
$totalquantity = $totalquantity + $product['quantity'];
}
}
else {
$totalquantity = 0;
}
echo $totalquantity;
?>
CSS:
/*----Add to cart Button-------*/
input.shopcart {
background-image: url(images/cart24.png);
background-color: transparent;
background-repeat: no-repeat;
background-position: 2px 5px 2px 5px;
vertical-align: middle;
width: 180px;
padding: 8px;
border-radius: 5px;
border: 1px solid #7ac9b7;
background-color: #12BDB8;
color: #F0F8FF;
font-weight: bold;
font-size: 15px;
cursor: pointer;
float: right;
text-align: center;
margin: 0px auto;
margin-top: -45px;
}
.shopcart:hover {
background-color: #10A8A3;
}
爲T這裏有更好的方法來完成這項工作?我在每個頁面上都有這個購物車按鈕,並且想要使用它,以便用戶在哪裏可以看到購物車中的物品數量。
快速端問題。我無法將我的購物車圖像設置爲位於左上角的我的網址背景。無論我如何處理背景定位,它都停留在那裏。任何人都看到我在做什麼錯了嗎?
***編輯,以顯示總量的位置可變
<form class="shopcart" id="block right"
action="shoppingcart.php">
<input type="submit" class="shopcart" name="shopcart" value="Shopping Cart">
</form>
</div>
</div>
</div>
<div class="whitepageOut">
<div class="whitepage">
<div class="content">
<h1>Shopping Cart</h1>
<!-- Add in additional info. I made the above up, to test. Correct when ready! -->
<div class="floatright">
<?php
echo "<p>
<a href='./shoppingcart.php?empty_cart=1'>Empty Cart</a>
</p>";
?>
</div>
<?php
// Initialize cart
if(!isset($_SESSION['shopping_cart'])) {
$_SESSION['shopping_cart'] = array();
}
// Update Cart
if(isset($_POST['update_cart'])) {
$quantities = $_POST['quantity'];
foreach($quantities as $id => $quantity) {
if(!isset($products[$id])) {
$message = "Invalid product!";
break;
}
$_SESSION['shopping_cart'][$id]['quantity'] = $quantity;
}
if(!$message) {
$message = "Cart updated!<br />";
}
}
// Empty cart
if(isset($_GET['empty_cart'])) {
$_SESSION['shopping_cart'] = array();
}
if(empty($_SESSION['shopping_cart'])){
echo "Your cart is empty<br />";
}
else {
echo $message;
?>
<form action='./shoppingcart.php?view_cart=1' method='POST'>
<table class="carttable">
<tr>
<th class="cartth">Name</th>
<th class="cartth">Price</th>
<th class="cartth">Category</th>
<th class="cartth">Quantity</th>
</tr>
<?php
$base_price = 0;
foreach($_SESSION['shopping_cart'] as $id => $product) {
$product_id = $product['product_id'];
$base_price += $products[$product_id]['price'] * $product['quantity'];
$shipping_price += $products[$product_id]['shippingprice'] * $product['quantity'];
?>
<tr>
<td class="carttd"><?php echo "<a href='./viewProduct.php?view_product=$id'>" . $product['name'];?><?php echo $products[$product_id]['name']; ?> </a>
</td>
<td class="carttd"><?php echo '$' . $products[$product_id]['price']; ?></td>
<td class="carttd"><?php echo $products[$product_id]['category']; ?></td>
<td class="carttd">
<?php echo "<input type='text' name='quantity[$product_id]' value='" . $product['quantity'] . "' />"; ?> </td>
</tr>
<?php
}
//Calculates total
$total_price += $base_price + $shipping_price;
?>
<tr>
<td colspan="2"></td>
<td class="tdcarttotal">Subtotal</td>
<td class="tdcarttotal"><?php echo "$" . $base_price; ?> </td>
</tr>
<tr>
<td colspan="2"></td>
<td class="tdcarttotal">Tax</td>
<td class="tdcarttotal">To be determined</td>
</tr>
<tr>
<td colspan="2"></td>
<td class="tdcarttotal">Shipping Price</td>
<td class="tdcarttotal"><?php echo "$" . $shipping_price; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td class="tdcarttotal">Total</td>
<td class="tdcarttotal"><?php echo "$" . $total_price; ?></td>
</tr>
</table>
<input type='submit' id='button' name='update_cart' value='Update Cart'>
</form>
<form action="checkout.php?checkout=1">
<input type="submit" class="checkoutbutton" value="Proceed to Checkout">
</form><br><br><br><br><br>
<?php
}
//Shopping Cart Quantity Count
if(isset($_SESSION['shopping_cart']) && is_array($_SESSION['shopping_cart'])) {
$totalquantity = 0;
foreach($_SESSION['shopping_cart'] AS $product) {
$totalquantity = $totalquantity + $product['quantity'];
}
}
else {
$totalquantity = 0;
}
echo $totalquantity;
?>
正確。我的購物車按鈕位於我的標題中。數量配置後,該變量將分配在頁面的底部。 – Paul
您需要在嘗試調用它之前分配變量。代碼按照它在文件中出現的順序進行處理。另一個解決方案是在變量設置後使用javascript設置提交按鈕的值。 –
我只是將我的變量移動到頁面頂部?我怎麼能用JS做到這一點。我的目標是在每一頁上都有這項工作。我添加了更多代碼來顯示展示位置。請參閱編輯。 – Paul