Page 1 of 1

Removing contents of a file after reading it

Posted: Fri Mar 18, 2005 5:51 pm
by rayner75
Hello, on my site i have a feedback forum
when the submit button is pressed it writes the fields into a text field then writes them out to a reciept.

Here is my code:

Code: Select all

<?php
		$name = $_POST['Name'];
		$email = $_POST['Email'];
		$age = $_POST['age'];
		$eqypt = $_POST['Egypt'];
		$china = $_POST['China'];
		$usa = $_POST['USA'];
		$europe = $_POST['Europe'];
		$rating = $_POST['Rating'];
		$comments = $_POST['Comments'];
		
		
      
      	$SEPARATOR = "::";
      
      	$filename = 'c3042575.txt';
		
      
      	$details = "Name: $name".$SEPARATOR.
       	           "Email: $email".$SEPARATOR.
       	           "Age: $age".$SEPARAT
                           "Egypt? $eqypt".$SEPARATOR.
	           "USA? $usa".$SEPARATOR.
	           "China? $china".$SEPARATOR.
	           "Europe? $europe".$SEPARATOR.
	           "Rating: $rating".$SEPARATOR.
                           "Comments: $comments".$SEPARATOR;      
      
       $file = fopen($filename,"a");
       fputs ($file, $details);
       fclose($file);      
      
       $file = fopen($filename,"r");
       $contents = fread($file, filesize($filename));
      
       $arrayContents = explode($SEPARATOR, $contents);      
      
       for ($i=0; $i<count($arrayContents); $i++)
       {
         echo ($arrayContents[$i]."<br />");
       }
?>
When i have read from the text file and displayed its contents how do i clear the text field?

Thanks for the help
Andy


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

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

Posted: Fri Mar 18, 2005 7:36 pm
by evilmonkey
If you want to clear the page, you can't. You're echo'ing the information directly to the browser. There's no real way to clear it other than to refresh the page or to redirect to another page. You may use the header() function in PHP (don't forget the output buffers) or <meta> tags.

If you want to clear the file, do this:

Code: Select all

<?php
...
$handle=fopen($file, "w");
fwrite($handle, "");
fclose($handle);
?>
And...boom, you're done. Good luck.


Good luck!


feyd | :roll:

Posted: Sat Mar 19, 2005 4:49 am
by rayner75
thanks it was to clear the text file :)

Posted: Sat Mar 19, 2005 1:00 pm
by evilmonkey
Glad to help, sorry feyd. Didn't know the PHP tags were back.