gpt4 book ai didi

php - 在表单输入中回显 php 作为带有路径的按钮

转载 作者:太空宇宙 更新时间:2023-11-04 11:57:20 25 4
gpt4 key购买 nike

我创建了一个购物车按钮,并在我的购物车页面中显示了总数量。我希望回显值出现在我的购物车按钮中,但是当我尝试将回显添加到该值时,它不起作用。是否有另一种方法来构建它以获取我的 url 图像、值:购物车和要显示的回显数量?

我的购物车按钮:

<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;
}

是否有更好的方法来完成这项工作?我在每个页面上都有这个购物车按钮,我想让用户无论身在何处都能看到购物车中的商品数量。

快速附带问题。我无法从左上角获取设置为我的 url 背景的购物车图像。无论我如何处理背景定位,它都会留在那里。有人看到我做错了什么吗?

***编辑以显示总量变量的位置

                    <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;
?>

最佳答案

在不显示更多代码的情况下,我的猜测是您试图在实际为变量赋值之前显示按钮。

编辑:

添加 javascript 来更新提交按钮的值:

更改 <input type="submit" class="shopcart" name="shopcart" value="Shopping Cart"><input type="submit" class="shopcart" name="shopcart" id="shopcart" value="Shopping Cart"> .

在您的 PHP 代码末尾添加:

echo "<script>document.getElementById('shopcart').value = 'Shopping Cart $totalquantity';</script>";

希望对你有帮助!

关于php - 在表单输入中回显 php 作为带有路径的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30103067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com