what wrong with my file?

Posted on 2005-02-24
Medium Priority
Last Modified: 2006-11-17

wat i trying to do here is .. reading content from a file and disply it.
and then get some input from the user and save it into file and display it.

this is the format of my record.txt

web1, www.web1.com // <--- one set of data
web2, www.web2.com 

my problem with my code is that...
i can't get my prog to write one set of data written line by line (one set of data per line)
but all my data will b written on one line even thought i have assigned "\n" to the back of my every set of data.

another prob
while using $_POST
whenever, i 'refresh' the screen, the data that has been post once (from submit-button), will b re-post again.
how can i prevent this from happening?

this is my code.


$my_array = localtime(time(), 1);
$month = $my_array["tm_mon"] + 1 ;
$day = $my_array["tm_mday"] ;
$year = $my_array["tm_year"] +1900 ;
$dayOfWeek = $my_array["tm_wday"] ;
$weekdays = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") ;

echo "<h3>Current Time : $month-$day-$year > $weekdays[$dayOfWeek]<br></h3>" ;


$fs = fopen( 'C:\Program Files\EasyPHP1-7\www\url\record.txt', 'r' );
//$fd = fopen( 'filedestination', 'w' );

while( ! feof( $fs ) )
      $tmp = fgets( $fs );

      # replace this line with whatever you want to do to each line
      //$tmp = process_line( $tmp );
      $process = explode(",", $tmp);
      echo "<a href = \"$process[1]\"> $process[0]</a> <br>";

      //fputs( $fd, $tmp );

//fclose( $fd );
fclose( $fs );

echo "<br>";

echo "<FORM ACTION=\"url.php\" METHOD=POST>";

echo " Comment <br>";
echo "<input type=text name = \"comment\" size=\"25\" ><br>";
echo " Address <br>";
echo "<input type=text name = \"url_address\" size=\"25\" ><br>";
echo "<INPUT TYPE=submit NAME=submit_button VALUE=\"Submit\">";
echo "</FORM>";

echo " -----------------<br>";

$tmp_comment = "";
$tmp_url_address = "";
$tmp_comment = $_POST["comment"];
$tmp_url_address = $_POST["url_address"];

$fd = fopen( 'C:\Program Files\EasyPHP1-7\www\url\record.txt', 'a' );

$write_out = $tmp_comment.",".$tmp_url_address;

fwrite ($fd, "\n$write_out\n");

fclose( $fd );

Question by:tom_mk
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 20

Expert Comment

ID: 13397239
in answser to the reposting question (part 2),
you need to have the screen that you redisplay not be the instant result of the posted data (that's why hitting refresh makes the data get re-entered).  There are a few ways to do this.  Probably the most prevelant is
a header redirect
('location:' . $_SERVER['REQUEST_URI');

in answer to part 1, try using the function nl2br($text)
when you are displaying it for the user.... (assuming this is a web thing).  "\n" don't mean jack for HTML display.

Author Comment

ID: 13397343
('location:' . $_SERVER['REQUEST_URI');  >> localtion is my websaddress?
LVL 20

Accepted Solution

virmaior earned 1500 total points
ID: 13397580
header('location:' . $_SERVER['REQUEST_URI]');

it's an internal PHP variable that reflects what page was asked for
if you just redirect to the same page, then they can't refresh (since there is no longer any POST data).

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question