fersick Posted August 17, 2011 Share Posted August 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244972-header-error-please-dont-flame-i-read-the-sticky/ Share on other sites More sharing options...
trq Posted August 17, 2011 Share Posted August 17, 2011 The error tells you where the problem is. Line 19 of products.php Please, in the future, we have tags. Quote Link to comment https://forums.phpfreaks.com/topic/244972-header-error-please-dont-flame-i-read-the-sticky/#findComment-1258369 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 header('Location: home.php'); You're using the header() command AFTER you've echo'ed content. Are you sure you read the sticky? Quote Link to comment https://forums.phpfreaks.com/topic/244972-header-error-please-dont-flame-i-read-the-sticky/#findComment-1258370 Share on other sites More sharing options...
skwap Posted August 17, 2011 Share Posted August 17, 2011 Just try to add below function at top of your code. ob_start(); Quote Link to comment https://forums.phpfreaks.com/topic/244972-header-error-please-dont-flame-i-read-the-sticky/#findComment-1258373 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Or arrange his logic properly? That solution is more like duct tape than a fix. Quote Link to comment https://forums.phpfreaks.com/topic/244972-header-error-please-dont-flame-i-read-the-sticky/#findComment-1258375 Share on other sites More sharing options...
fersick Posted August 17, 2011 Author Share Posted August 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/244972-header-error-please-dont-flame-i-read-the-sticky/#findComment-1258377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.