Strange problem when writing to file

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
aleetk
Forum Newbie
Posts: 5
Joined: Thu Oct 06, 2005 4:10 am

Strange problem when writing to file

Post by aleetk »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


hi!

i am still pretty new to php but i managed to write a simple html form page that allows someone to key in his name and then click the submit button. Once the form is submitted, i have a php file to write the name to a text file. I've another form that reads the name from the file and prints it to the browser screen. The funny thing here is that when it prints to the screen, I get sort of an extra line below the name printed. For example in the html form page, I key in the name John and click the submit button. When I call the readform.php, i should see the output as Name: John but my output is showing as:

Name: John
Name:

There is an extra name: as shown above. Maybe you guys can help me solve this problem. I have included all the files and the code in this post below.

HTML Form - form.htm

Code: Select all

<form name="form1" method="post" action="/Dennis/phptest/submitform.php">
<table width="100%"  border="0">
  <tr>
    <th width="5%" scope="row"><div align="left"><label>Name:</label></div></th>
    <th width="95%" scope="row"><div align="left">
      <input type="text" name="name">
    </div></th>
  </tr>
</table>
<p>
  <label></label>
</p>
<p>
  <label>
  <input type="submit" name="Submit" value="Submit">
  </label>
</p>
</form>
The php file that writes the name to a file - submitform.php

Code: Select all

<?php

$filename = "form.txt";

$myFile = fopen($filename, "w");

fputs($myFile, $_POST['name'] . "\n");

fclose($myFile);

?>
The php file that reads the name from a file and prints it to the browser screen - readform.php

Code: Select all

<?php

$filename = "form.txt";

$myFile = fopen($filename, "r");

while (!feof($myFile))
{
	$myLine = fgets($myFile, 255);
	print("Name: " . "$myLine<br/>");
}

fclose($myFile);

?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
aleetk
Forum Newbie
Posts: 5
Joined: Thu Oct 06, 2005 4:10 am

Post by aleetk »

It's alright guys. I've found a solution to it :)
Post Reply