Forum Moderators: coopster

Message Too Old, No Replies

post to text file

         

mtmt

4:34 am on Apr 9, 2006 (gmt 0)

10+ Year Member



i am looking for a very simple php script that will take submitted form data and place it at the top of a text file

like this

form


<form>
<input name="a1" type="text" />
<input name="b2" type="text" />
<input name="c3" type="text" />
<input name="d4" type="button" />
</form>

and write it to the top of a text file

<div>
<div>a1</div>
<div>b2</div>
<div>c3</div>
</div>

i cant find any thing like this on the net
some help please

dreamcatcher

7:33 am on Apr 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi mtmt,

Welcome to Webmaster World. :)

Have you thought about giving this a try yourself? You will need PHP`s fopen, fwrite and fclose functions.

So, something like:


<?php

//Directory to write to..
$dir = 'files/';

if (isset($_POST['d4']))
{
$string = "<div>\n";
$string .= "<div>".$_POST['a1']."</div>\n";
$string .= "<div>".$_POST['b2']."</div>\n";
$string .= "<div>".$_POST['c3']."</div>\n";
$string .= "</div>";

$fp = fopen($dir.'/data.txt', 'ab');

if ($fp)
{
fwrite($fp,$string);
fclose($fp);

echo 'Data written ok..<br><br>';

}

}

<form method="POST" action="index.php">
<input name="a1" type="text" />
<input name="b2" type="text" />
<input name="c3" type="text" />
<input name="d4" type="button" />
</form>

grandpa

7:34 am on Apr 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<umm>

mtmt

4:35 pm on Apr 9, 2006 (gmt 0)

10+ Year Member



thanks im kinda new to php i just needed something to start off with

mtmt

8:17 pm on Apr 9, 2006 (gmt 0)

10+ Year Member



the codes not working. what did i do wrong?

<form method="POST" action="index.php">
<input name="a1" type="text" value="a1" />
<input name="b2" type="text" value="b2" />
<input name="c3" type="text" value="c3" />
<input name="d4" type="button" value="d4" />
</form>

<?php

if (isset($_POST['d4']))
{
$string = "<div>\n";
$string .= "<div>".$_POST['a1']."</div>\n";
$string .= "<div>".$_POST['b2']."</div>\n";
$string .= "<div>".$_POST['c3']."</div>\n";
$string .= "</div>";

$fp = fopen('txt.txt', 'ab');

if ($fp)
{
fwrite($fp,$string);
fclose($fp);

echo 'Data written ok..';

}

}

?>

grandpa

8:49 pm on Apr 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try changing this line:
<input name="d4" type="button" value="d4" />

to:

<input name="d4" type="submit" value="d4" />

mtmt

8:54 pm on Apr 9, 2006 (gmt 0)

10+ Year Member



just one more question. is there any way to make the newly submitted data print to the first line of the text file instead of the last line?

grandpa

9:06 pm on Apr 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<umm>

I had a suspicion that we would come back to this question :) In a nutshell, I don't think so. If you look at the attributes for fopen or fwrite, you'll see that the file is typically opened with the file pointer set at the end of the file (for append) or at the beginning of the file (for write), and in the latter case you are overwriting the existing file.

But it CAN be done. It's not a single step, however. What I would do is read the existing file, possibly into an array. Then write the new entries, followed by writing the previously read data from the file. My logic for this would look something like:

a) read file
b) store data somewhere (array would work)
c) write new data (essentially creating a new file)
d) write the stored data