govindJ Posted August 24, 2014 Share Posted August 24, 2014 (edited) I am trying to create a program to get input from user in textarea and save that input into a text file. But this code is only creating an empty file. <?php echo <<<_END <form action="form.php" method="post"> <textarea name="textdata" cols="50" rows="20"></textarea> <input type="submit" name="submit" value="CLICK ME"> <input type="hidden" name="submit_check" value="1"> </form> _END; function write(){ $fname="test.txt"; $fh=fopen($fname,'w'); $data=$_POST['textdata']; fwrite($fname,$data); fclose($fh); } if(isset($_POST['submit_check'])){ write(); } ?> please help me. sorry for bad english Edited August 24, 2014 by mac_gyver code tags please Quote Link to comment https://forums.phpfreaks.com/topic/290626-saving-content-from-textarea-into-a-file/ Share on other sites More sharing options...
Solution fastsol Posted August 24, 2014 Solution Share Posted August 24, 2014 I think you're using the wrong var in your frwite(). You open the file with the handle of $fh but then try to use the $fname in the function to write it. Try this fwrite($fh,$data); Example info http://php.net/manual/en/function.fwrite.php Quote Link to comment https://forums.phpfreaks.com/topic/290626-saving-content-from-textarea-into-a-file/#findComment-1488802 Share on other sites More sharing options...
govindJ Posted August 25, 2014 Author Share Posted August 25, 2014 Thank you man it worked. Quote Link to comment https://forums.phpfreaks.com/topic/290626-saving-content-from-textarea-into-a-file/#findComment-1488829 Share on other sites More sharing options...
cyberRobot Posted August 25, 2014 Share Posted August 25, 2014 For what it's worth, fopen(), fwrite(), and fclose() could be replaced with a call to file_put_contents(). More information can be found here: http://php.net/manual/en/function.file-put-contents.php Quote Link to comment https://forums.phpfreaks.com/topic/290626-saving-content-from-textarea-into-a-file/#findComment-1488841 Share on other sites More sharing options...
govindJ Posted August 25, 2014 Author Share Posted August 25, 2014 file_put_contents() function is very handy :happy-04: Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/290626-saving-content-from-textarea-into-a-file/#findComment-1488867 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.