Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML

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"/>
</form>
$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);
    fclose($fh);
    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.