简体   繁体   English

将html表单数据保存到文件

[英]Saving html form data to file

I am using Trisquel 7.0 .我正在使用Trisquel 7.0 I've some basic knowledge about html and JavaScript.我有一些关于 html 和 JavaScript 的基本知识。 Now I want to save html form data to file (Also interested in loading/filling html form from file).现在我想将 html 表单数据保存到文件(也有兴趣从文件加载/填充 html 表单)。

I searched and found that This would be possible with php etc. But I don't know How to.我搜索并发现这可以通过 php 等实现。但我不知道如何。 As a beginner at this time, I would like to save only text information in text-file simply from html form.作为此时的初学者,我想仅将文本信息从 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 mytext to text-file file, say mytext.txt .现在我想将文本从mytext保存到文本文件中,比如mytext.txt All data/files are accessed locally on my PC.所有数据/文件都在我的 PC 上本地访问。 So, How can I do that?那么,我该怎么做呢? With PHP or JavaScript?;使用 PHP 还是 JavaScript? then How?那么如何? (give me some basic script/information). (给我一些基本的脚本/信息)。


Also Suggest me external resource for learning interaction html form with database.还建议我学习与数据库交互的 html 表单的外部资源。

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.该文件本身需要是一个 php 文件。
  • There is no form-validation implemented in this example此示例中没有实现表单验证
  • The webserver needs write permissions in $targetFolder网络服务器需要 $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 中存储对象

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM