Jump to content

PHP reading data from a file after writing, BIG problem pls help


nvidia

Recommended Posts

Hi, i have form wherebye people select values via radio buttons(note: first value is CHECKED) and when they press the submit button, these values r then stored written to a text file. So far i can write then values to a text file. However, i can't seem to read this values to display on my webpage. Pls help.
form1.html file
[code]
<form method="post" action="handler4.php">
      <table border cellpading = "2">
Mr.X <input type="radio" name="select" value="Mr.X " CHECKED> <br />
Tom Cruis <input type="radio" name="select" value="Tom Cruis " ><br />

<input type=submit  value="Press me!"  />
  </form>
[/code]

handler4.php
[code]
<?php

session_start();
$selection = $_POST['select'];
$contents = 0;
// store location of file in variable
$filename = "/home/eland/u1/kbccs/w1009048/public_html/Internet_Pg/C2/C2.txt";

$fp = fopen($filename,'a+'); // a+  open for reading + writing
                              // place file pointer at EOF

$write = fwrite($fp, "$selection\r\n");
$contents = fread( $fp,filesize($filename) );
fclose($fp);
?>
The contents of the file $contents is : <? echo $contents ?>

[/code]

Please note, with regards to the function $fp = fopen($filename,'a+');, i've tried using 'r', but i cannot see any data in the file. I've also tried 'r+' which works in that i can write data to my C2.txt file, but when selecting a second value, it is placed on the top line which i can see fully, but the first value is placed on a second but i can only see partially e.g
Mr.x // most recent clicked i.e second value.
ise // first value
Therefore 'a+' is the only way i can FULLY write data to file.
As a checker to see it actually reads, <? echo $contents ?> does not show anything. Does anybody know y??
Why dont you use [url=http://www.php.net/manual/en/function.file-get-contents.php]file_get_contents()[/url] and [url=http://www.php.net/manual/en/function.file-put-contents.php]file_put_contents()[/url] ? They will make your life easier :)

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.