2

I am using Trisquel 7.0. I've some basic knowledge about html and JavaScript. Now I want to save html form data to file (Also interested in loading/filling html form from file).

I searched and found that This would be possible with php etc. But I don't know How to. As a beginner at this time, I would like to save only text information in text-file simply from html form.

Below I am writing simple example with simple html form and JavaScript function (without any action)

<html>
<form name=myform>
<input type=text name=mytext>
<input type=button value=save onClick=saving()>
</form>
<script>
function saving()
{
}
</script>
</html>

Now I want to save text from mytext to text-file file, say mytext.txt. All data/files are accessed locally on my PC. So, How can I do that? With PHP or JavaScript?; then How? (give me some basic script/information).


Also Suggest me external resource for learning interaction html form with database.

Pandya
  • 198
  • 2
  • 4
  • 19
  • Do you want to store the Data serverside (with PHP) or Clientside (with Javascript)? And for what reason? Maybe Cookies are an better Idea (Cookies are also just Text Files, but easier to access) – j_s_stack Jan 26 '15 at 07:20
  • @j_s_stack At this time I want to save data to my PC locally. (probably Client-side) – Pandya Jan 26 '15 at 07:22
  • Have a look here: http://www.w3schools.com/js/js_cookies.asp – j_s_stack Jan 26 '15 at 07:29
  • Here it tells you how to use it to save Form Data: http://www.the-art-of-web.com/javascript/setcookie/ – j_s_stack Jan 26 '15 at 07:31
  • Will go with the answer by Mario A. Since you mention you want to store the files locally, to run PHP on a local machine, you might want to set it up, you cannot use it "simply" with a browser like you do with html and javascript. If you are using Windows, XAMPP will be a good start for a local setup. Or WAMP. Or Node.js. Also adding to j_s_stack's answer, please know, cookies can be removed when you clear your browser cookies. Might be a bad way to store data if you want them to be unaffected by browser actions. – Whirl Mind Jan 26 '15 at 07:36
  • 1
    PHP would also be the better solution if you want to upgrade to an Database Version later on. – j_s_stack Jan 26 '15 at 07:41

2 Answers2

8

If you want to save the form data on the server side, I would do it with php like this:

<?php
$action = $_GET["action"];
$myText = $_POST["mytext"];

if($action = "save") {
  $targetFolder = "/path/to/folder";
  file_put_contents($targetFolder."mytext.txt", $myText);
}
?>   
<html>
<head>
 <title>myform</title>
</head>
<body>
  <form action="?action=save" name="myform" method="post">
    <input type=text name="mytext">
    <input type="submit" value="save">
  </form>
</body>
</html>
  • The file itself needs to be a php file.
  • There is no form-validation implemented in this example
  • The webserver needs write permissions in $targetFolder

However if you want to save the data on the client side, normally you do it with local storage. But mind that only the client can access data of his local storage. Here is an example: Storing Objects in HTML5 localStorage

Community
  • 1
  • 1
Mario A
  • 3,286
  • 1
  • 17
  • 22
0

To save data without database or backend, you can services that provide just that. 1. PageClip 2. Usebasin 3. FormKeep 4. netlify 5. Formcarry

Follow this blog link to see details http://rohitvinay.com/how-to-store-contact-form-data-without-backend/

Rohit Vinay
  • 665
  • 7
  • 38
  • Sadly, your blog link is dead; it returned Error 404 (not found). It would be great if you could update it. – EJ Mak Nov 15 '22 at 21:25