Jump to content

Header error... please don't flame. I read the sticky


fersick

Recommended Posts

I have a function that will generate a table of products. Each <tr> is a form of its own. When i log in to my site and start a session the user can add products to there cart.

 

This all works fine except if you try and add a second product from the same page. If you do this you get an error :

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\workspace\Assignment3\global\products.php:19) in C:\xampp\htdocs\workspace\Assignment3\global\products.php on line 66

 

 

CODE: SELECT ALL

function products($product) {
   $file = "data/products.txt";
   $fp = fopen($file, 'r');

   $count = 0;
   while (!feof($fp)) {

      $data = fgets($fp);
      $section = explode(":" , $data);

      if($product == $section[0]) {
         echo '
      <form action="" method="post" onsubmit=""> <br />
         <table>
            <tr class="order">
               <td class="item1"><img src="'. $section[1] . '"
                   height="94" width="96"></img></td>
               <th class="itemHeader">';

         $productName = $section[2];
         echo $productName . '<input type="hidden" name="Product' . $count . '" value="' . $productName. '" />
                  </th>
               <td class="product"><b>'.$section[3] .':</b> ' . $section[4] . '<br />
                  <b>' . $section[5]; 

         // used because of : delimiter could have used something else..
         if ($product == 'Soap') {
            echo ':</b> ' . $section[6] . '<br /></td>' ;
         } else {
            echo '</b>' . $section[6] . '<br /></td>';
         }

         echo '<td class="product"><b>Price: ';

         $productPrice = $section[7];

         echo '$' . $productPrice . '</b><input type="hidden" name="Price' . $count . '" value="' . $productPrice.'" />
                     </td>';
         if (!empty($_SESSION['userName'])) {
            echo '<td class="submitItem"><input type="text" size="3" maxlength="3"
                  name="Qty'. $count . '"value="0" /> ';
            echo '<input type="submit" value="Add to Cart" name="submit' . $count . '" id="submitItem" />';
         }

         echo '</td>
            </tr>
         </table>
      </form>';

         // stores the required information in Session variables
         if (isset($_POST['submit' . $count])) {

            $index = $count;

            $_SESSION[$product . 'Name' . $index] = $_POST['Product' . $index];
            $_SESSION[$product .'Price' . $index] = $_POST['Price' . $index];

            if (empty($_SESSION['soapQty' . $index])) {
               $_SESSION[$product . 'Qty' . $index] = $_POST['Qty' . $index];
            } else {
               $_SESSION[$product . 'Qty' . $index] = $_SESSION[$product . 'Qty' . $index] + $_POST['Qty' . $index];
            }
            $_SESSION['cart'] = true;
            $_SESSION['globalQty'] += $_SESSION[$product . 'Qty' . $index];
            header('Location:  home.php');
            exit;
         }
      }
      $count++;
   }

   $_SESSION[$product . 'Count'] = $count;
}

 

So basically the first call to this function's isset works but any consecutive call to isset for another product of the same type doesn't.

 

I have read about white space and text before a header call but can't see how that is the problem here. Also read the sticky.

 

Any help would be greatly appreciated

 

p.s. I appologise for the $_SESSION variable declaration as a chain and NOT as an array for me to change my code in my assignment at this stage would take too long.

 

 

Link to comment
Share on other sites

Just try to add below function at top of your code.

 

ob_start();

 

thank for this.

 

Regarding the other posts. Thanks also. With the echo before the header. I thought that this only was an issue directly before the header call. guess not. Thanks

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.