简体   繁体   English

以形式输入的Echo php充当具有路径的按钮

[英]Echo php in a form input acting as a button with a path

I created a shopping cart button and in my shopping cart page I have a total quantity count that I echo out. 我创建了一个购物车按钮,在我的购物车页面上,我有一个总数量回显出来。 I want the echo'd value to be in my Shopping Cart button, but when I try to add in my echo to the value, it doesn't work. 我希望将回显的值放在“购物车”按钮中,但是当我尝试将回显添加到该值时,它不起作用。 Is there another way to structure this to get my url image, value: Shopping Cart, and the echo'd quantity to show? 是否有另一种方法来构造此结构以获取我的网址图片,值:购物车以及要显示的回显数量?

My shopping Cart button: 我的购物车按钮:

<form class="shopcart" id="block right" action="shoppingcart.php">
    <input type="submit" class="shopcart" name="shopcart" value="Shopping Cart <?php echo $totalquantity; ?> ">
</form> 

The total quantity count: 总数量计数:

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

Is there a better approach to this to make this work? 有没有更好的方法可以使这项工作呢? I have this shopping cart button on every page and want to make it so where ever the user is they will see the quantity of what is in their shopping cart. 我在每个页面上都有此购物车按钮,并希望这样做,以便无论用户在哪里,他们都将看到他们购物车中的数量。

Quick side question. 快速的侧面问题。 I am unable to get my shopping cart image that is set to my url background out of the top left corner. 我无法从左上角获取设置为url背景的购物车图像。 No matter what I do with the background positioning it stays there. 不管我对背景定位做什么,它都停留在那儿。 Anyone see what I'm doing wrong with it? 有人看到我在做什么错吗?

***Edited to show placement of total quantity variable ***编辑以显示总数量变量的位置

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

Without showing more of the code, my guess would be that you are trying to show the button before the variable is actually assigned a value. 在不显示更多代码的情况下,我的猜测是您正在尝试在实际为变量分配值之前显示按钮。

Edit: 编辑:

Adding javascript to update the value of the submit button: 添加JavaScript以更新“提交”按钮的值:

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

Add this at the end of your PHP code: 在您的PHP代码末尾添加以下代码:

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

Hope this helps you! 希望这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM