0

Having difficulty identifying what is throwing the error. Have removed all white space. Using CodeAnywhere as my IDE as it is a requirement for me. Any help would be appreciated.

Full error:

Warning: Cannot modify header information - headers already sent by (output started at /home/cabox/workspace/Project/ShoppingCart.php:134) in /home/cabox/workspace/Project/ShoppingCart.php on line 169

Code:

<?php 
session_start();
$connect = mysqli_connect("localhost", "root", "", "lab"); //connect to the db

if(isset($_POST["add_to_cart"])) //if the add to cart value is set
{
    if(isset($_SESSION["shopping_cart"])) //if the shopping cart session is set
    {
        $item_array_id = array_column($_SESSION["shopping_cart"], "item_id"); 
        //if there are values in your shopping cart loop through and display them
        if(!in_array($_GET["id"], $item_array_id))
        {
            $count = count($_SESSION["shopping_cart"]);
            $item_array = array(
                'item_id'           =>  $_GET["id"],
                'item_name'         =>  $_POST["hidden_name"],
                'item_price'        =>  $_POST["hidden_price"],
                'item_quantity'     =>  $_POST["quantity"]
            );
            $_SESSION["shopping_cart"][$count] = $item_array;
        }
        else
        {
            echo '<script>alert("Item Already Added")</script>'; 
            //restriction on design - can only add one instance of each product, delete if you want to add more
        }
    }
    else
    {
        $item_array = array(
            'item_id'           =>  $_GET["id"],
            'item_name'         =>  $_POST["hidden_name"],
            'item_price'        =>  $_POST["hidden_price"],
            'item_quantity'     =>  $_POST["quantity"]
        );
        $_SESSION["shopping_cart"][0] = $item_array;
    }
}

if(isset($_GET["action"]))
{
    if($_GET["action"] == "delete") 
        //when you select 'Remove', delete the values based on id. Alert box will show on completition
    {
        foreach($_SESSION["shopping_cart"] as $keys => $values)
        {
            if($values["item_id"] == $_GET["id"])
            {
                unset($_SESSION["shopping_cart"][$keys]);
                echo '<script>alert("Item has been removed from Shopping Cart")</script>';
                echo '<script>window.location="ShoppingCart.php"</script>';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Shopping Cart</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
        <!-- AJAX is about loading data in the background and display it on the webpage, without reloading the whole page.-->
    <link rel="stylesheet" type="text/css" href="thePerfectPair.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

    <script>
                // Get the modal
                var modal = document.getElementById('id01');

                // When the user clicks anywhere outside of the modal, close it
                window.onclick = function(event) {
                    if (event.target == modal) {
                        modal.style.display = "none";
                    }
                }
    </script> 
    </head>
    <body>
        <nav class="navigationBar">
        <!--Establishing the top left hand side of the Navigation Bar-->
        <div class="leftTopNavBar">
            &euro; EUR |
            <!--The following produces a pop up sign up form that is validated through javascript functions in the following code.
            I implemented this pop up form to create a different design element as opposed to just referring users to another page.
            All design element are assigned in the css file and it involved calling many different classes in order not to conflict
            with other forms and input boxes contained in the website-->
    <button onclick="document.getElementById('id01').style.display='block'" style="width:auto;" class="SignUpBtn0">SIGN UP |</button>
    <div id="id01" class="modal">
      <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
        <form class="modal-content" action="/action_page.php">
          <div class="container1">
            <h1>Sign Up</h1>
            <p>Please fill in this form to create an account with Perfect Pair!</p>
            <hr>
            <b>Email</b>
            <input type="text" placeholder="Enter Email" name="email" required>
            <b>Password</b>
            <input type="password" placeholder="Enter Password" name="psw" required>
            <b>Repeat Password</b>
            <input type="password" placeholder="Repeat Password" name="psw-repeat" required>
            <label>
                <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me</label>
            <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms &amp; Privacy</a>.</p>
            <div class="clearfix">
                <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
                <button type="submit" class="signupbtn">Sign Up</button>
                </div>
            </div>
        </form>
    </div>
            SAVED ITEMS
        </div>
        <!--Establishing the right side of the navBar-->
        <div class="rightTopNavBar">
            <input type="search" placeholder="Search..." name="search">
            <button type="submit"><i class="fa fa-search"></i></button>
            <a href="LogIn.php">LOGIN |</a> CART
            <i class="fa fa-shopping-cart" style="font-size:20px"></i>
        </div>
        <!--Establishing the middle of the navBar-->
        <div class="middleTopNavBar">
            <a href="ShoeHomePage.php"><img src="Images/perfectPairLogo.png" class="logoHeader" alt="logo" width="100" height="100"></a>
        </div>
        <!--Establishing bottom navBar-->
        <div class="bottomNavBar">
      <a href="ShoeHomePage.php" style="text-decoration:none"> <i class="fa fa-home" style="font-size:36px">&emsp;</i></a>
      <a href="Heels.php" style="text-decoration:none">&emsp;HEELS&emsp;</a>
      <a href="Sandals.php" style="text-decoration:none">&emsp;SANDALS&emsp;</a>
      <a href="Boots.php" style="text-decoration:none">&emsp;BOOTS&emsp;</a>
      <a href="Trainers.php" style="text-decoration:none">&emsp;TRAINERS&emsp;</a>
      <a href="Flats.php" style="text-decoration:none">&emsp;FLATS&emsp;</a>
      <a href="ShoppingCart.php" style="text-decoration:none">&emsp;SHOPPING CART&emsp;</a></div></nav>
        <div class="container">
            <br><?php $query="SELECT*FROM tbl_product1 ORDER BY id ASC"; $result=mysqli_query($connect, $query); if(mysqli_num_rows($result)>0){while($row=mysqli_fetch_array($result)){?><div class="col-md-4">
<form method="post" class="phpForm" action="ShoppingCart.php?action=add&id=<?php echo $row["id"];?>" >
                    <div class="formStyleDiv" style=" background-color:#f1f1f1; border-radius:1px; padding:16px; width:25%;" align="center">
                        <img src="Images/<?php echo $row["image"];?>" class="img-responsive" style="height: 200px;" />
                        <h4 class="text-info"><?php echo $row["name"];?></h4><br />
                        <h4 class="text-danger">&euro;<?php echo $row["price"];?></h4>
                        <input type="text" name="quantity" value="1" class="form-control" />
                        <input type="hidden" name="hidden_name" value="<?php echo $row["name"];?>" />
                        <input type="hidden" name="hidden_price" value="<?php echo $row["price"];?>" />
                        <input type="submit" name="add_to_cart" style="margin-top:5px;" class="submitBtn" class="btn btn-success" value="Add to Cart" /></div>
                </form>
            </div><?php
                    }
                }
            ?><div style="clear:both"></div>
            <br />
            <h3>Order Details</h3>
            <div class="table-responsive" >
                <table>
                    <tr>
                        <th width="40%" align="left">Item Name</th>
                        <th width="10%" align="left">Quantity</th>
                        <th width="20%" align="left">Price</th>
                        <th width="15%" align="left">Total</th>
                        <th width="5%" align="left">Action</th>
                    </tr><?php if(!empty($_SESSION["shopping_cart"])) { $total = 0; foreach($_SESSION["shopping_cart"] as $keys => $values){?><tr>
                        <td><?php echo $values["item_name"];?></td>
                        <td><?php echo $values["item_quantity"];?></td>
                        <td>&euro;<?php echo $values["item_price"];?></td>
                        <td>&euro;<?php echo number_format($values["item_quantity"] * $values["item_price"], 2);?></td>
                        <td><a href="ShoppingCart.php?action=delete&id=<?php echo $values["item_id"];?>"><span class="text-danger">Remove</span></a></td>
                    </tr><?php $total = $total + ($values["item_quantity"] * $values["item_price"]);}?><tr>
                        <td colspan="3" align="right">Total</td>
                        <td align="right">&euro;<?php echo number_format($total, 2); ?></td>
                        <td></td>
                    </tr><?php $cookie_name="Total"; setcookie($cookie_name, $total, time()+(86400*30),"/");}?></table></div>
            <input type="button" class="submitFormBtn" name="submit" value= "Submit" onclick="window.open ('success.php','_self',false)" /><br />
        </div>
    </div>
    <br />
    </body>
</html>
RobC
  • 22,977
  • 20
  • 73
  • 80
  • 1
    The error message is telling you that "output started at /home/cabox/workspace/Project/ShoppingCart.php:134". What does that line look like? – Greg Schmidt Apr 21 '18 at 16:07
  • 2
    Possible duplicate of [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Nigel Ren Apr 21 '18 at 16:12
  • @GregSchmidt line 134:
    " >
    – 114318516 Apr 21 '18 at 16:19
  • 1
    Seems that the line down at the end, where you're trying to set a cookie, would be line 169? Cookies would need to be set before you start sending output. So, either reorganize your code so you calculate the total and create the cookie right at the start, or else build up all the output in a string (with a heredoc, for example, or the `ob_*` functions), then you can still set the cookie at the end of your code and then output the string all at once. – Greg Schmidt Apr 21 '18 at 20:33

0 Answers0