Page 1 of 1

Form to File - Problem

Posted: Tue Jun 10, 2008 3:29 pm
by Rust1
Hello, I'm pretty new to this. But, After I submit form, I get a error message:
Error Message -
Warning: Cannot modify header information - headers already sent by (output started at /home/*******/public_html/writefile.php:6) in /home/********/public_html/writefile.php on line 30
Any Ideas?

Form -

Code: Select all

<HTML>
<BODY BGCOLOR=2222222>
<HEAD>
 <TITLE>PCID Tool</TITLE>
</HEAD>
<BODY>
<center><a href="PCIDTool.php"><b><FONT COLOR="#ffffff">PCID Tool</Font></b></a> - <b><a href="http://thsquad.com"><FONT COLOR="#ffffff">Team Hate</FONT></a><b> - <a href="BL.php"><b><FONT COLOR="#ffffff">NovaWorld Banned List</FONT></a></b> - <b><a href="upload/"><b><FONT COLOR="#ffffff">Screen Shots</FONT></b></a></b></b></center><p>
<center><form method="post" action="writefile.php">
<TABLE style="background : #E0E0E0; border: solid 2pt #111111">
<TR>
    <TD colspan='2' style="border-bottom: solid 1pt #666666; font-weight: bold; text-align: center">
    User Data:
    </TD>
 </TR>
 <TR>
    <TD>
    PCID:
    </TD>
    <TD>
    <INPUT TYPE="TEXT"  name="PCID:" size="10" maxlength="10" value=''>
    </TD>
  </TR>
  <TR>
    <TD>
    Player Name:
    </TD>
    <TD>
    <INPUT TYPE="TEXT" name="Gname:" size="20" maxlength="15" value=''>
    </TD>
  </TR>
  <TR>
    <TD>
    Forum Name:
    </TD>
    <TD>
    <INPUT TYPE="TEXT"   name="Fname:" size="20" maxlength="15" value=''>
    </TD>
  </TR>
  <TR>
    <TD>
    Other names:
    </TD>
    <TD>
    <INPUT TYPE="TEXT" name="Onames:" size="20" maxlength="20" value=''>
    </TD>
  </TR>
  <TR>
    <TD>
    <INPUT TYPE="SUBMIT" value="Submit Search">
    </TD>
  </TR>
</TABLE>
</center>
</BODY>
</BODY BGCOLOR>
</HTML>
writefile.php -

Code: Select all

<HTML>
<HEAD>
 <TITLE>Processing</TITLE>
</HEAD>
<BODY>
<?php
 
// FILENAME - writefile.php
 
if ($_POST['submit'] == "Submit") {
    $dir = 'ID/';
    $filename = "PCID01.php";
 
    $content = $_POST['Gname:']." <br> ";
    $content .= $_POST['Fname:']." <br> ";
    $content .= $_POST['Onames:']."<br>";
    $content .= $_POST['PCID:']."<br>____________________________________<br>";
 
    if (!$handle = fopen($dir . $filename, "a+")) {
        echo 'Unable to suscessfully open file.: ' . $filename;
        exit;
    }
    if (!fwrite($handle, $content)) {
        echo 'Unable to suscessfully write to file: ' . $filename;
        exit;
    }
 
    echo 'Successfully added ID to Database, Click <a href="ID/PCID01.php">Here</a> to view database. ' . $filename;
} else {
    header("Location: pcid.php");
}
 
?>
</BODY>
</HTML>

Re: Form to File - Problem

Posted: Tue Jun 10, 2008 3:41 pm
by califdon
Once you have sent anything to the browser, you cannot send a header(). Your script begins with <HTML>, so that means that you cannot send a header anywhere later in that script.

Re: Form to File - Problem

Posted: Tue Jun 10, 2008 3:59 pm
by Rust1
califdon wrote:Once you have sent anything to the browser, you cannot send a header(). Your script begins with <HTML>, so that means that you cannot send a header anywhere later in that script.
How would I fix this? Would I remove the <HTML> tags?

Re: Form to File - Problem

Posted: Tue Jun 10, 2008 4:24 pm
by califdon
Rust1 wrote:
califdon wrote:Once you have sent anything to the browser, you cannot send a header(). Your script begins with <HTML>, so that means that you cannot send a header anywhere later in that script.
How would I fix this? Would I remove the <HTML> tags?
No. You cannot use the header() function in that manner. You need to reverse your logic and begin by testing whether the $_POST['Submit'] value is acceptable, and if not, send the header. Then the else part of your script is where you begin sending your HTML.

Re: Form to File - Problem

Posted: Tue Jun 10, 2008 4:46 pm
by superdezign
Rust1 wrote:How would I fix this? Would I remove the <HTML> tags?
Headers are a hidden portion of the request, similar to file headers.
i.e. BMP file headers include the size of the file

Headers MUST be sent before any other portion of the file.