Javascript 将 html 表单数据保存到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28146044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:16:33  来源:igfitidea点击:

Saving html form data to file

javascriptphphtmlforms

提问by Pandya

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).

我正在使用Trisquel 7.0。我有一些关于 html 和 JavaScript 的基本知识。现在我想将 html 表单数据保存到文件中(也对从文件加载/填充 html 表单感兴趣)。

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.

我搜索并发现这可以使用 php 等。但我不知道如何。作为此时的初学者,我只想将文本信息仅从 html 形式保存在文本文件中。

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

下面我正在用简单的 html 表单和 JavaScript 函数编写简单的示例(没有任何操作)

<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 mytextto 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).

现在我想将文本保存mytext到文本文件文件,比如mytext.txt. 所有数据/文件都在我的 PC 上本地访问。那么,我该怎么做呢?使用 PHP 还是 JavaScript?那么如何?(给我一些基本的脚本/信息)。



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

还建议我学习与数据库交互的 html 表单的外部资源。

回答by Mario A

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

如果你想在服务器端保存表单数据,我会用这样的 php 来做:

<?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
  • 该文件本身需要是一个 php 文件。
  • 在这个例子中没有实现表单验证
  • 网络服务器需要在 $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

但是,如果您想在客户端保存数据,通常使用本地存储进行。但请注意,只有客户端才能访问其本地存储的数据。这是一个示例: 在 HTML5 localStorage 中存储对象

回答by Rohit Vinay

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

要在没有数据库或后端的情况下保存数据,您可以提供仅提供这些的服务。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/

按照此博客链接查看详细信息http://rohitvinay.com/how-to-store-contact-form-data-without-backend/