Form to File - Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rust1
Forum Newbie
Posts: 2
Joined: Tue Jun 10, 2008 3:18 pm

Form to File - Problem

Post 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>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Form to File - Problem

Post 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.
Rust1
Forum Newbie
Posts: 2
Joined: Tue Jun 10, 2008 3:18 pm

Re: Form to File - Problem

Post 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?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Form to File - Problem

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Form to File - Problem

Post 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.
Post Reply