如何重定向结帐页面检查购物车空的状况?

时间:2015-12-17 04:48:05

标签: php

我已经声明了一个变量$counter = 1;,我已经将条件置于else条件下。如果$counter= 0;。它应该重定向到header("Location:" . BASE_URL . "product.php");

但我收到的错误如下!

  

警告:无法修改标头信息 - 已在D:\ wamp \ www \ project \ ali \ products中发送的标头(在D:\ wamp \ www \ project \ ali \ views \ header.php:72处开始输出)第125行的\ check_out.php

以下是check_out.php页面的代码!

<?php
require_once '../models/user.php';
require_once '../models/brand.php';
require_once '../models/cart.php';
//require_once '../models/item.php';
require_once '../views/top.php';
require_once '../models/place_order.php';
 $counter = 1; // initial value
?>
</head>

<body>
    <?php
    if (!$obj_user->login) {
        //redirect to the sign up page e.g
        header("Location:" . BASE_URL . "signup.php");
    }
    ?>
    <div id="wrapper">

        <?php
        require_once '../views/header.php';
        ?>


        <!-- **************************Main************************************ -->



        <div id="main">

            <div id="col-main">
                <?php
                require_once '../views/middle_left.php';
                ?>
                <div id="middle-right">
                    <h3 class="text-center">Check Out</h3><hr>

                    <div class="row">
                        <div class="col-lg-12">
                            <h4>Billing Information</h4>
                            <span>Welcome <?php echo($obj_user->full_name); ?> Online Computer Shop</span>
                        </div>
                    </div>    
                    <hr>
                    <div class="row">

                        <div class="col-lg-12"> 
                            <form class= "form-horizontal" action='" . BASE_URL . "products/process/process_cart.php' method='post'>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="billing_name">Name</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="billing_name" name="billing_name" value="" placeholder="Enter Name" >
                                        <span id="billing_name_error">
                                            <?php
                                            if (isset($errors['billing_name'])) {
                                                echo ($errors['billing_name']);
                                            }
                                            ?>
                                        </span>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="billing_address">Address</label>
                                    <div class="col-sm-10">
                                        <textarea class="form-control" id="billing_address" name="billing_address" value="" placeholder="Address"></textarea>                                
                                        <span id="billing_address_error">
                                            <?php
                                            if (isset($errors['billing_address'])) {
                                                echo ($errors['billing_address']);
                                            }
                                            ?>
                                        </span>
                                    </div>
                                </div>
                            </form>
                        </div> 
                    </div>
                    <hr>
                    <br>
                    <div class="shopping-background">
                        <!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
                        <?php
                        if ($obj_cart->items) 
                            {
                            echo("<form action='" . BASE_URL . "products/process/process_cart.php' method='post'>"
                            . "<table class='table table-hover' width='400px' border='1px' bordercolor='black' cellspacing='0' cellpadding='0'>");
                            echo("<thead>"
                            . "<tr align='center'>"
                            . "<th>Product Count</th>"
                            . "<th>Product Name</th>"
                            . "<th>Quantity</th>"
                            . "<th>TOTAL</th>"
                            . "</tr>"
                            . "</thead>");


                            foreach ($obj_cart->items as $item) {
                                echo("<tbody>"
                                . "<tr align='center'>"
                                . "<td>$counter</td>"
                                . "<td>$item->item_name</td>"
                                . "<td>$item->quantity</td>"
                                //   . "<td><input class='box' type='text' value='$item->quantity' name='qtys[$item->itemID]'></td>"
                                . "<td>$item->total_price</td>"
                                . "</tr>"
                                . "</tbody>");
                                $counter++; // update counter value on each product
                            }
                            echo("<thead>"
                            . "<tr align='right' valign='middle'>"
                            . "<th></th>"
                            . "<th></th>"
                            . "<th></th>"
                            . "<th align='center'>$obj_cart->total_price</th>"
                            . "</tr>"
                            . "</thead>");

                            echo("</table></form>");
                        }  else {
                            $counter == 0;
                            //echo("<label>Your cart is empty</label>");

                            header("Location:" . BASE_URL . "product.php");

                        }

                        ?>

                        <!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-sm-3 col-sm-offset-2">

                            <a href="<?php echo(BASE_URL); ?>products/products.php"><input class='btn btn-default' type='button' value='Shop More' /></a>
                        </div>
                        <div class="col-sm-3 col-sm-offset-2">
                            <a href="<?php echo(BASE_URL); ?>"><input class='btn btn-default' type='button' value='Place Order' /></a>
                        </div>

                        <hr>
                    </div>
                    <br><br>
                </div>
            </div>  
        </div>
    </div>
    <!--***************************End of Main************************************ -->

    <!-- ***************************Footer************************************ -->
    <?php
    require_once '../views/footer.php';
    ?>

    <!-- *************************Footer************************************** -->



</body>
</html>

3 个答案:

答案 0 :(得分:1)

How to fix "Headers already sent" error in PHP

基本上,将输出存储在某个变量中,并且只有在知道自己没有重定向后才能将其输出。如果您想制作自己的模板系统,http://php.net/manual/en/function.ob-get-contents.php也可能有用。

答案 1 :(得分:0)

根据PHP Manual

  必须在发送任何实际输出之前调用

header()   普通的HTML标记,文件中的空行或PHP。

改为使用

<?php
    if (!$obj_user->login) {
        //redirect to the sign up page e.g
    echo "<script>location.href='" . BASE_URL . "signup.php'</script>";
    }
    ?>

和其他部分:

else {
  $counter == 0;
  //echo("<label>Your cart is empty</label>");
  echo "<script>location.href='" . BASE_URL . "product.php'</script>";
}

或者您可以使用:

ob_start()

你可能想读这个。 http://php.net/manual/en/function.ob-start.php

答案 2 :(得分:0)

您可以使用此<script>location.href="<?php echo BASE_URL.'product.php' ?>";</script> 代替标题重定向(&#34;位置:&#34; .BASE_URL。&#34; product.php&#34;); < / p>