为什么我的提交按钮没有重定向

时间:2015-10-19 09:58:59

标签: php

我有一个带有提交按钮的表单。

<input type="submit" name="submitOrder" id="submitOrder" value="Purchase" class="btn btn-warning">

在下面我有几行代码:

<?php
    if (isset($_POST['submitOrder'])) {
        header ("location:payment.php?discount=". $discountCode ."");
    }
?>

点击时为什么按钮,除了重新加载页面什么都不做?我希望它将用户重定向到payment.php?

包含提交按钮的整个表单...

<form method="post" action="cart_update.php">
    <div class="row" style="padding-bottom: 10px;">
        <div class="col-md-2">
            <strong>Quantity</strong>
        </div>
        <div class="col-md-3">
            <strong>Name</strong>
        </div>
        <div class="col-md-2">
            <strong>Price</strong>
        </div>
        <div class="col-md-2">
            <strong>Total</strong>
        </div>
        <div class="col-md-3">
            <strong>Remove</strong>
        </div>
    </div>

    <?php
        if(isset($_SESSION["cart_products"])) { // Check session var
            $total = 0; // Set initial total value

            foreach ($_SESSION["cart_products"] as $cart_itm) {
                // Set variables to use in content below
                $ProductName = $cart_itm["ProductName"];
                $product_qty = $cart_itm["product_qty"];
                $Price = $cart_itm["Price"];
                $ProductCode = $cart_itm["ProductCode"];
                $subtotal = ($Price * $product_qty); // Calculate Price x Qty
                echo '<div class="row" style="padding: 10px 0;">';
                echo '<div class="col-md-2">Qty: <input type="text" size="2" maxlength="4" name="product_qty['. $ProductCode .']" value="'. $product_qty .'" /></div>';
                echo '<div class="col-md-3">Product Name: '. $ProductName .'</div>';
                echo '<div class="col-md-2">Price: '. $currency . $Price .'</div>';
                echo '<div class="col-md-2">Total: £';
                echo money_format('%i', $subtotal);             
                echo '</div>';                      
                echo '<div class="col-md-3"><input type="checkbox" name="remove_code[]" value="'. $ProductCode .'" /></div>';
                echo '</div>';
                echo '<hr>';
                $total = ($total + $subtotal); // Add subtotal to total var
                $grand_total = $Price * $product_qty;
            }

            $grand_total = $total + $shipping_cost; // Grand total including shipping cost
            foreach($taxes as $key => $value) { // List and calculate all taxes in array
                $tax_amount = round($total * ($value / 100));
                $tax_item[$key] = $tax_amount;
                $grand_total = $grand_total + $tax_amount;  // Add tax val to grand total
            }

            $list_tax = '';

            foreach($tax_item as $key => $value) { // List all taxes
                $list_tax .= $key. ' : '. $currency . sprintf("%01.2f", $value).'<br />';
            }

            $shipping_cost = ($shipping_cost) ? 'Shipping Cost: ' . $currency . sprintf("%01.2f", $shipping_cost).'<br />':'';
        }
    ?>

    <br />

    <div class="row">
        <span style="float: right; text-align: right; width: 100%;">Amount Payable: <strong>£<?php echo sprintf("%01.2f", $_SESSION['grand_total'] = $grand_total); ?><br />Price inc. VAT and Postage + Packaging</strong><br />Discount Code: <input type="text" name="discountCode" id="discountCode" value="TEST" /></span>

        <br />
        <br />

        <a href="index.php" class="btn btn-warning">Continue Shopping</a>
        <button type="submit" class="btn btn-warning">Update</button>
        <!-- <a href="payment.php" class="btn btn-warning">Purchase</a> -->
        <input type="submit" name="submitOrder" id="submitOrder" value="Purchase" class="btn btn-warning">

        <?php
            if (isset($_POST['submitOrder'])) {
                header ("location:payment.php?discount=". $discountCode ."");
            }
            $_SESSION['ProductName'] = $ProductName;
            $_SESSION['product_qty'] = $product_qty;
            $_SESSION['grand_total'] = $grand_total;
        ?>
    </div>

    <input type="hidden" name="return_url" value="<?php $current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); echo $current_url; ?>" />
</form>

2 个答案:

答案 0 :(得分:0)

问题是,在发送重定向标头之前有输出,因为你的条件在html部分。

在将任何输出发送到客户端之前,您应该使用重定向放置代码。 另一种选择是打开输出缓冲,但在将某些内容发送给客户端之前,还必须这样做。

答案 1 :(得分:-1)

确保返回的第一个项目是没有空格或其前任何内容的标题,即回声或文本,

检查日志文件中是否已发送错误

确保按钮位于表单