chloebyte Posted November 17, 2015 Share Posted November 17, 2015 (edited) For some reason its not saving input to the text file. <form action="edit.php" method="post"> <textarea Name="update" cols="50" rows="10"></textarea>Enter text here<br/> <input type="submit" value="update"/> </form> <?php $_POST['update']; $file1 = "file.txt"; $file = fopen($file1, "w+"); $contents = file_get_contents($file1); file_put_contents($file1, $contents); if($_POST['update']) fwrite($file, $file1); fclose($file); ?> anyone know why?;/ Edited November 17, 2015 by chloebyte Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted November 17, 2015 Solution Share Posted November 17, 2015 1) You have not assigned this $_POST['update']; to a variable. I presume that line is meant to read as $update = $_POST['update']; 2) Open the file use append mode $file = fopen($file1, "a"); Then there is no need for this $contents = file_get_contents($file1); file_put_contents($file1, $contents); 3) fwrite takes the file handler as the first argument and the value to be written as the second argument (needs to be $update not $file1) fwrite($file, $update); The alternative is not use fopen/fwrite/fclose but to use only file_put_content using the FILE_APPEND flag if(isset($_POST['update'])) { $file = "file.txt"; $update = $_POST['update']; file_put_contents($file, $update, FILE_APPEND); // append $update the file.txt } Quote Link to comment Share on other sites More sharing options...
chloebyte Posted November 17, 2015 Author Share Posted November 17, 2015 1) You have not assigned this $_POST['update']; to a variable. I presume that line is meant to read as $update = $_POST['update']; 2) Open the file use append mode $file = fopen($file1, "a"); Then there is no need for this $contents = file_get_contents($file1); file_put_contents($file1, $contents); 3) fwrite takes the file handler as the first argument and the value to be written as the second argument (needs to be $update not $file1) fwrite($file, $update); The alternative is not use fopen/fwrite/fclose but to use only file_put_content using the FILE_APPEND flag if(isset($_POST['update'])) { $file = "file.txt"; $update = $_POST['update']; file_put_contents($file, $update, FILE_APPEND); // append $update the file.txt } Thank you very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.