Page 1 of 1

header location problem

Posted: Wed Oct 24, 2007 10:21 am
by monkeymafia
hi

Once users have filled in a form and pressed the submit button i want them to be redirected to a confirmation page. However its throwing me the following error:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/kumar/public_html/Storm Broadband/useraccount.php:18) in /home/kumar/public_html/Storm Broadband/useraccount.php on line 187
this is my code

Code: Select all

<?php


mysql_connect("localhost", "?", "?") or die(mysql_error());
mysql_select_db("?") or die(mysql_error());



?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Storm Broadband</title>
<script language="javascript" src="list.js"></script>

<link rel="stylesheet" href="storm.css" type="text/css" media="screen" />
</head>

<body onload="fillCategory();">

<div id="container">

<div id="sitename">
<h1></h1>
<h2></h2>
</div>

<div id="bottomsection">
</div>

<div id="mainmenu">
<ul>
<li><a href="index.html">Home</a></li>
<li><a class="current" href="myaccount.html">My account</a></li>
<li><a href="#">Webmail</a></li>
<li><a href="Contact.html">Contact Us</a></li>
</ul>
</div>
 
<div id="wrap">

<div id="leftside">



<div id="side-nav">

<div class="navborder">
<h1 class="orange">Storm Packages</h1>

<a href="payg.html" class="orange">PAYG</a>
<a href="homepackage.html" class="orange">Home</a>
<a href="#" class="orange">Office</a>
</div>
<br />
<div class="navborder">
<h1 class="orange">Sign up</h1>

<a href="#" class="orange">Order</a>
<a href="#" class="orange">Migrate</a>
</div>

</div>
</div>



<div id="content2">

<h1>Successful Login</h1>

<?php
    echo "Welcome ";
    echo $_GET['memberid'];

?>
<br />
<br />
<?php

if (!isset($_POST['submit'])) {
?>



<FORM name="drop_list" action="useraccount.php" method="POST" >



<table width="660" cellpadding="0" cellspacing="0"><tr><td align=left>

<input type="hidden" name="action" value="tickets"><input type="hidden" name="id" value="new">
<table width="660" cellpadding="4" cellspacing="1" bgcolor="#3366CC">
  <tr>
    <td width="20%" bgcolor="#CAE4FF"><strong>Username</strong></td>
    <td width="80%" bgcolor="#FFFFFF"><input name="username" type="text" size="50" readonly value="<?php echo $_GET['memberid']?>"</td>
  </tr>
  <tr>
    <td width="20%" bgcolor="#CAE4FF"><strong>Subject</strong></td>
    <td width="80%" bgcolor="#FFFFFF"><input name="subject" type="text" size="50"></td>
  </tr>
  <tr>
    <td bgcolor="#CAE4FF"><strong>Category</strong></td>
    <td bgcolor="#FFFFFF">
    
<SELECT  NAME="category" onChange="SelectSubCat();" >
<Option value="">Category</option>
</SELECT>&nbsp;
<SELECT id="SubCat" NAME="subcat">
<Option value="">SubCat</option>
</SELECT>

	</td>
  </tr>
  <tr>
    <td bgcolor="#CAE4FF"><strong>Priority</strong></td>
    <td bgcolor="#FFFFFF"><select name="priority">
      <option value="QUERY" selected>QUERY</option>
      <option value="LOW">LOW</option>

      <option value="MEDIUM">MEDIUM</option>
      <option value="HIGH">HIGH</option>
    </select></td>
  </tr>
  <tr>
    <td bgcolor="#CAE4FF"><strong>Tel No. </strong></td>
    <td bgcolor="#FFFFFF"><input name="telno" type="text" id="telno" size="20"></td>

  </tr>
  <tr>
    <td bgcolor="#CAE4FF"><strong>E-Mail address</strong></td>
    <td bgcolor="#FFFFFF"><input name="email" type="text" id="email" size="40" value="<?php echo $row['email'] ?>" /></td>
  </tr>
  <tr>
    <td colspan="2" align="center" bgcolor="#FFFFFF">
    <br><b>Message</b><br><textarea name="message" cols="100" rows="10" class="message"></textarea><br></td>
      </tr>
      <tr>
    <td colspan="2" align="center" bgcolor="#FFFFFF"><b>Attachment</b><br>
    <br><input name="attach" type="file" id="attach"></td>
  </tr>
  <tr>

    <td colspan="2" align="center" bgcolor="#FFFFFF"><input type="submit"  value="Create ticket" name ="submit" onClick="return validate_form()" class="submit2";></td>
  </tr>
</table>
<br><br>
</td></tr></table><br>	</td>
  </tr>
</table>
<br /></td>
              </tr>
            </table></td>
          </tr>

          <tr>
            <td height="13" background="page_bottom.gif"></td>
          </tr>
        </table></td>
      </tr>
    </table>
    </td>
  </tr>

      </table>
  </form>

  
  <?php
}
 else {
          $userid = $_POST['username'];
          $subject = $_POST['subject'];
          $cat = $_POST['category'];
          $subcat = $_POST['subcat'];
          $priority = $_POST['priority'];
          $tel = $_POST['telno'];
          $email = $_POST['email'];
          $message = $_POST['message'];
          
mysql_query("INSERT INTO technicalproblems (fk_memberid2, subject, cat1, cat2, priority, tel, email, message) 
VALUES ('" . mysql_real_escape_string($userid) . "', '" . mysql_real_escape_string($subject) . "', '" . mysql_real_escape_string($cat) . "', '" . mysql_real_escape_string($subcat) . "', '" . mysql_real_escape_string($priority) . "', '" . mysql_real_escape_string($tel) . "', '" . mysql_real_escape_string($email) . "', '" . mysql_real_escape_string($message) . "')");      
  header('Location: http://www.kumar.adsl24.co.uk/Storm%20B ... memberid=' . $memberid);
  }
  ?>






</div>

</div>
</div>
<div class="clearingdiv">&nbsp;</div>
<div id="footer">&copy; 2007 Storm Broadband Limited | Design by MK</div>
</body>
</html>
the header code is towards the bottom of the code. any help greatly appreciated. thanks

Posted: Wed Oct 24, 2007 10:52 am
by shannah
The header() call must be sent before any output is sent to the browser. That is the source of the error.

You can sort of bypass this by adding:

Code: Select all

ob_start();
to the beginning of your script to enable output buffering (the causes output to be buffered until the script is finished).

Posted: Wed Oct 24, 2007 10:54 am
by shannah
Also it is a good idea to call

Code: Select all

exit;
after you call header('Location:...') to prevent the script from executing unintended parts.

Posted: Wed Oct 24, 2007 10:55 am
by Stryks
That headers message just means that you sent output to the browser and then sent a header. As the name implies, headers must be BEFORE ANYTHING ELSE.

Frankly, I think your code is upside-down. Well, to the way I code anyhow.

What you want to do is have all handling code (especially forms or other processes where something will be done and then redirected on failure or success) at the top of the page. That way, if all the code is run and a redirect hasn't been sent, then (and only then) start building the page.

Oh, and you'll want to send and exit(); after that header.

Hope this helps.

Posted: Wed Oct 24, 2007 10:56 am
by monkeymafia
thank you! did the trick nicely :)

Posted: Wed Oct 24, 2007 9:03 pm
by Stryks
I know ob_start() can get you around this issue, but I seriously think it's a bad idea. It's like using the error suppressor (@) on a function that is throwing an error instead of working out what is wrong.

All you need, as I have said above, is a simple code shift so that your processing code is ABOVE the presentation code. There is even the added benefit that all of the logic is contained at the top of the page. No more need to be wading through HTML to fine little blocks of php that may or may not contain the flawed code.

Here ... your code reorganized.

Code: Select all

<?php

mysql_connect("localhost", "?", "?") or die(mysql_error());
mysql_select_db("?") or die(mysql_error());

if(isset($_POST['submit'])
	  $userid = $_POST['username'];
	  $subject = $_POST['subject'];
	  $cat = $_POST['category'];
	  $subcat = $_POST['subcat'];
	  $priority = $_POST['priority'];
	  $tel = $_POST['telno'];
	  $email = $_POST['email'];
	  $message = $_POST['message'];
	 
	 mysql_query("INSERT INTO technicalproblems (fk_memberid2, subject, cat1, cat2, priority, tel, email, message)
		VALUES ('" . mysql_real_escape_string($userid) . "', '" . mysql_real_escape_string($subject) . "', '" . mysql_real_escape_string($cat) . "', '" . mysql_real_escape_string($subcat) . "', '" . mysql_real_escape_string($priority) . "', '" . mysql_real_escape_string($tel) . "', '" . mysql_real_escape_string($email) . "', '" . mysql_real_escape_string($message) . "')");     
	 header('Location: http://www.kumar.adsl24.co.uk/Storm%20Broadband/formsubmittednotice.php?memberid=' . $memberid);
         exit();   // must have this
  }

  // If the code makes it this far, then clearly the form wasn't submitted, so we're free to show everything else. 
  // No need to but it in an else block either - because if the submit code it not run, this runs anyhow.
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Storm Broadband</title>
<script language="javascript" src="list.js"></script>

<link rel="stylesheet" href="storm.css" type="text/css" media="screen" />
</head>

<body onload="fillCategory();">

<div id="container">

<div id="sitename">
<h1></h1>
<h2></h2>
</div>

<div id="bottomsection">
</div>

<div id="mainmenu">
<ul>
<li><a href="index.html">Home</a></li>
<li><a class="current" href="myaccount.html">My account</a></li>
<li><a href="#">Webmail</a></li>
<li><a href="Contact.html">Contact Us</a></li>
</ul>
</div>
 
<div id="wrap">

<div id="leftside">



<div id="side-nav">

<div class="navborder">
<h1 class="orange">Storm Packages</h1>

<a href="payg.html" class="orange">PAYG</a>
<a href="homepackage.html" class="orange">Home</a>
<a href="#" class="orange">Office</a>
</div>
<br />
<div class="navborder">
<h1 class="orange">Sign up</h1>

<a href="#" class="orange">Order</a>
<a href="#" class="orange">Migrate</a>
</div>

</div>
</div>



<div id="content2">

<h1>Successful Login</h1>

<?php
    echo "Welcome ";
    echo $_GET['memberid'];

?>
<br />
<br />

<FORM name="drop_list" action="useraccount.php" method="POST" >



<table width="660" cellpadding="0" cellspacing="0"><tr><td align=left>

<input type="hidden" name="action" value="tickets"><input type="hidden" name="id" value="new">
<table width="660" cellpadding="4" cellspacing="1" bgcolor="#3366CC">
  <tr>
    <td width="20%" bgcolor="#CAE4FF"><strong>Username</strong></td>
    <td width="80%" bgcolor="#FFFFFF"><input name="username" type="text" size="50" readonly value="<?php echo $_GET['memberid']?>"</td>
  </tr>
  <tr>
    <td width="20%" bgcolor="#CAE4FF"><strong>Subject</strong></td>
    <td width="80%" bgcolor="#FFFFFF"><input name="subject" type="text" size="50"></td>
  </tr>
  <tr>
    <td bgcolor="#CAE4FF"><strong>Category</strong></td>
    <td bgcolor="#FFFFFF">
   
<SELECT  NAME="category" onChange="SelectSubCat();" >
<Option value="">Category</option>
</SELECT>&nbsp;
<SELECT id="SubCat" NAME="subcat">
<Option value="">SubCat</option>
</SELECT>

        </td>
  </tr>
  <tr>
    <td bgcolor="#CAE4FF"><strong>Priority</strong></td>
    <td bgcolor="#FFFFFF"><select name="priority">
      <option value="QUERY" selected>QUERY</option>
      <option value="LOW">LOW</option>

      <option value="MEDIUM">MEDIUM</option>
      <option value="HIGH">HIGH</option>
    </select></td>
  </tr>
  <tr>
    <td bgcolor="#CAE4FF"><strong>Tel No. </strong></td>
    <td bgcolor="#FFFFFF"><input name="telno" type="text" id="telno" size="20"></td>

  </tr>
  <tr>
    <td bgcolor="#CAE4FF"><strong>E-Mail address</strong></td>
    <td bgcolor="#FFFFFF"><input name="email" type="text" id="email" size="40" value="<?php echo $row['email'] ?>" /></td>
  </tr>
  <tr>
    <td colspan="2" align="center" bgcolor="#FFFFFF">
    <br><b>Message</b><br><textarea name="message" cols="100" rows="10" class="message"></textarea><br></td>
      </tr>
      <tr>
    <td colspan="2" align="center" bgcolor="#FFFFFF"><b>Attachment</b><br>
    <br><input name="attach" type="file" id="attach"></td>
  </tr>
  <tr>

    <td colspan="2" align="center" bgcolor="#FFFFFF"><input type="submit"  value="Create ticket" name ="submit" onClick="return validate_form()" class="submit2";></td>
  </tr>
</table>
<br><br>
</td></tr></table><br>  </td>
  </tr>
</table>
<br /></td>
              </tr>
            </table></td>
          </tr>

          <tr>
            <td height="13" background="page_bottom.gif"></td>
          </tr>
        </table></td>
      </tr>
    </table>
    </td>
  </tr>

      </table>
  </form>
</div>

</div>
</div>
<div class="clearingdiv">&nbsp;</div>
<div id="footer">&copy; 2007 Storm Broadband Limited | Design by MK</div>
</body>
</html> 
  
?>
You see?

If it doesn't do the processing, it shows the whole page, and if it does, it never makes it far enough to output anything.

Have a look and have a think about using this logic instead of the ob_start() workaround.

Cheers

Posted: Thu Oct 25, 2007 4:27 am
by monkeymafia
thanks i reorganized it the first time you suggested :wink: it was horrible and unreadable before 8O
thanks again!