Javascript 从网站存储数据的最简单方法(在服务器端)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13973963/
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
Easiest way to store data from web site (on server side)
提问by kardanov
I have very simple web site (which is actually single page), there is one input field and a button.
I need to store data submitted by users somewhere on server side. Perfect way could be simple text file and new lines appended to it after each button click. Log file will be also ok.
As I understand it is not possible with JavaScript itself. I'm looking for easiest solution, preferably with no server-side programming (but if it is required, it should be as easy as possible and work out-of-box). I can use some side service if it could be helpful.
Please help.
Thanks in advance.
我有一个非常简单的网站(实际上是单页),有一个输入字段和一个按钮。
我需要将用户提交的数据存储在服务器端的某个地方。完美的方式可以是简单的文本文件,并在每次单击按钮后附加新行。日志文件也可以。
据我了解,JavaScript 本身是不可能的。我正在寻找最简单的解决方案,最好没有服务器端编程(但如果需要,它应该尽可能简单并且开箱即用)。如果有帮助,我可以使用一些辅助服务。
请帮忙。
提前致谢。
UPD. Just want to rephrase the main question. I do not really need to store something on server side. I need to collect some input from users. Is it possible? It would also be ok if it for example will be just sent to my e-mail.
更新。只是想改写主要问题。我真的不需要在服务器端存储一些东西。我需要从用户那里收集一些输入。是否可以?例如,如果它只是发送到我的电子邮件,那也可以。
采纳答案by Brad Christie
For a very simple form-to-server-log script:
对于一个非常简单的表单到服务器日志脚本:
Your form:
您的表格:
<form action="save-to-log.php" method="POST">
<fieldset>
<legend>Add to log</legend>
<p>
Message:
<textarea name="message"></textarea>
</p>
<p>
<input type="submit" value="SAVE" />
</p>
</fieldset>
</form>
Then save-to-log.php
然后保存到log.php
<?php
$log_file_name = 'mylog.log'; // Change to the log file name
$message = $_POST['message']; // incoming message
file_put_contents($log_file_name, $message, FILE_APPEND);
header('Location: /'); // redirect back to the main site
if it's a unix host you'll need to add 755 permissions to the directory of the log so PHP has access to write to it. Other than that, you'll have a form that keeps appending information to mylog.log.
如果它是 unix 主机,则需要向日志目录添加 755 权限,以便 PHP 有权对其进行写入。除此之外,您将拥有一个不断将信息附加到mylog.log.
Follow-Up
跟进
If you don't necessarily need it store on the server (you mentioned email) you can use the following instead as the PHP script:
如果您不一定需要将它存储在服务器上(您提到了电子邮件),您可以使用以下内容作为 PHP 脚本:
<?php
$to_email = '[email protected]';
$subject = 'User feedback from site';
$message = $_POST['message'];
// this may need configuring depending on your host. If you find the email isn't
// being sent, look up the error you're receiving or post another question here on
// SO.
mail($to_email, $subject, $message);
header('Location: /');
回答by Pez Cuckow
You can't store information on the server without some sort of server side script.
如果没有某种服务器端脚本,您就无法在服务器上存储信息。
There are twodifferent places to store data, on the client and on the server.
有两个不同的地方可以存储数据,在客户端和服务器上。
On the clientside, there are lots of ways from cookies to Store.js, however it sounds like you want to store the information on the server.
在客户端,从 cookie 到Store.js有很多方法,但是听起来您想将信息存储在服务器上。
To store on the serveryou need some sort of application that can receive posts from javascript/http and save them in a file.
要存储在服务器上,您需要某种可以从 javascript/http 接收帖子并将它们保存在文件中的应用程序。
In your case a very simple PHP script like the below would be perfect:
在您的情况下,像下面这样的非常简单的 PHP 脚本将是完美的:
<?php
//Was the request (post or get) parameter data supplied?
if(!empty($_REQUEST['data']) {
$file = 'log.txt';
$data = $_REQUEST['data']."\n";
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
}
?>
回答by stamat
How about dumping JSON to a file with PHP and then loading it on request?
如何使用 PHP 将 JSON 转储到文件中,然后根据请求加载它?
回答by user3124464
If you want to get the data sent to your email address, there are several free services that can do this without installing any server side applications... A PHP or CGI script is still being used but it is hosted by the service, not by you.,
All you have to do is paste some code into your site and then all submitted data will be sent to your email address.,
A lot of people don't have the know-how to do this on their own, or their hosting service will not allow send-mail to work. Thats why these services exist. And of course most of them are supported by ads that are placed in the email that you get from the form.
Anyway, here is the link for a good service I found. You can also Google "Free Form Processing" to find more.
如果您想将数据发送到您的电子邮件地址,有几个免费服务可以在不安装任何服务器端应用程序的情况下执行此操作...仍在使用 PHP 或 CGI 脚本,但它由服务托管,而不是由你., 你所要做的就是将一些代码粘贴到你的网站上,然后所有提交的数据都将发送到你的电子邮件地址。,
很多人不知道如何自己或他们的托管服务将不允许发送邮件工作。这就是这些服务存在的原因。当然,其中大多数都由放置在您从表单中获得的电子邮件中的广告支持。
无论如何,这是我找到的优质服务的链接。您还可以谷歌“自由表单处理”以查找更多信息。
https://secure.tectite.com/hf/auth/GetStarted?WWWTECTITE
https://secure.tectite.com/hf/auth/GetStarted?WWWTECTITE
Hope this helps.
希望这可以帮助。

