Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


HTML Forms to print to text file

Okay, so I've made my form using the tutorials posted here. I would like the data inside the form to be saved to a text file on my server computer.

I had considered using PHP to e-mail it, however I can't quite get it to work, and I'd rather it just save a new text file. Same thing for using

<form action="MAILTO:xxx@xxx.com">

If you know the video I should watch, or have a solution I would be very grateful.

I'm guessing it's something to do with JScript, but I'm unsure

2 Answers

Hi Curtis, I don't think Treehouse has a video specifically in regards to HTML form to a text file. However, below I have included a script from Stack Overflow.

<form id="some" name="someName" method="post" action="/ur/url/to/post">
   <input type="text" id="some1" class="someClass" value="" name="fileWrite"/>
   <iput type="submit" value="submit" class="submitClass"/>
$myFile = "testFile.txt";
if(isset($_POST['fileWrite']) && !empty($_POST['fileWrite'])) {
      $fileWrite = $_POST['fileWrite'];
if($fileWrite) {
    $fh = fopen($myFile, 'a') or die("can't open file"); //Make sure you have permission
    fwrite($fh, $fileWrite);
    exec('/your/command /dev/null 2>/dev/null &');

—Stack Overflow

I'm assuming this is JavaScript yes?

It's PHP and most likely the best solution. However, below are a few other ways using JavaScript. The last one uses Node.js, although, I'm not sure exactly how to implement that.