The file is saving but I'm getting this error too? Notice: Undefined index: saving in C:\wamp\www\appendfile\test.php on line 162 Any suggestions? I can't see what's wrong... This is where line 162 is: Thanks <td style="color:white;"> <?php $saving = $_REQUEST['saving']; if ($saving == 1) { $data = $_POST['data']; $file = "test.txt"; $fp = fopen($file, "w") 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!"; } ?> <form name="form1" method="post" action="test.php?saving=1"> <textarea name="data" cols="82" rows="20"> <?php $file = "test.txt"; if (!empty($file)) { $file = file_get_contents("$file"); echo $file; } ?> </textarea> <br> <center><input type="submit" value="Save Testimonials"></center> </form> </td>
if this is where the error happens: $_REQUEST['saving']; Then it means that the "index" named "saving" doesn't exists in the array REQUEST. $_REQUEST will look in the URL to see a parameter named "saving", something like this : http://www.mysite.com/page.php?saving=123 if it's possible that the parameter is not there in your URL sometimes, just look if it exists :
Another way is to use the isset() function: $saving = ""; if (isset($_REQUEST['saving'])) { $saving = $_REQUEST['saving']; } PHP: isset() is about 3 times faster than array_key_exists(). They do virtually the same thing but optimisation is never a bad thing.
Either will work, they both achieve the same solution using different methods. It's down to personal preference which one you adopt.