Saving to text file in php

Associate
Joined
6 Mar 2009
Posts
495
Hi, im have a form in php that users are to enter information about themselves and when they click submit button i would like the data to be stored into a text file.

Could someone give me some pointers as to go about it please.
 
You need something like this
PHP:
<?php
$saving = $_REQUEST['saving'];
if ($saving == 1){
$data = $_POST['data'];
$file = "data.txt";

$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");

fclose($fp);
echo "Saved to $file successfully!";

}
?>
 
Put something similar (you need to change a few variables to make them specific to your script) on the page the form action leads to, ie:

Code:
<form action="example.php" method="post">

If your form led to example.php, then hargi's (modified) code would be on example.php.

:)

Have a look on php.net for the mentioned variables to find out what each bit does.
 
Back
Top Bottom