Page 1 of 1

HTML: Save text on server?

Posted: Wed Oct 09, 2013 1:07 pm
by dj1437
Hello. I need help with a little project:

There's a website with a textbox and a submit button. When the user clicks the submit button the text should be saved into a txt file on the server. I remember doing such a thing a very long time ago and php was involved too but now I forgot it and there don't seem to be any good answers on google either. Can you help with it?

Re: HTML: Save text on server?

Posted: Wed Oct 09, 2013 2:00 pm
by Filip
Hello,


This code should work:
Code: Select all
<?
if ($_POST && $_POST['nameoffield']!="") {
    file_put_contents("path/to/file.txt", $_POST['nameoffield']."\n", FILE_APPEND);
}
?>
<form method="post">
<input type="text" name="nameoffield" />
<input type="submit" value="Submit" />
</form>
KR
-Filip

Re: HTML: Save text on server?

Posted: Wed Oct 09, 2013 3:46 pm
by dj1437
For some reason, the text file stays empty. The text file is in the same folder as the index.php so I set the path to something like 'file.txt'

Re: HTML: Save text on server?

Posted: Wed Oct 09, 2013 3:49 pm
by dj1437
dj1437 wrote:
For some reason, the text file stays empty. The text file is in the same folder as the index.php so I set the path to something like 'file.txt'
Edit: forgot to CHMOD it! Working now! Thanks a lot.

Re: HTML: Save text on server?

Posted: Wed Oct 09, 2013 5:08 pm
by Filip
You're welcome,

If you need any help, feel free to ask.

-Filip