回声php在表单输入中充当带路径的按钮

时间:2015-05-07 13:48:36

标签: php html css forms

我创建了一个购物车按钮,在我的购物车页面中,我有一个总数量,我回应。我希望echo的值在我的购物车按钮中,但是当我尝试将回声添加到值时,它不起作用。有没有其他方法来构建这个以获取我的网址图像,值:购物车,以及要显示的回显数量?

我的购物车按钮:

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

有没有更好的方法来使这项工作?我在每个页面上都有这个购物车按钮,并希望这样做,以便用户可以看到购物车中的数量。

快速提问。我无法从左上角获取设置为我的网址背景的购物车图片。无论我如何处理背景定位,它都会留在那里。任何人都能看到我做错了什么?

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

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

1 个答案:

答案 0 :(得分:1)

在没有显示更多代码的情况下,我的猜测是您在变量实际分配值之前尝试显示按钮。

编辑:

添加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>";

希望这能帮到你!