简体   繁体   English

HTML表单保存到服务器端文本文件

[英]HTML Form save to server-side text file

All. 所有。

I've found many-a scripts which save form input to the local computer, IE downloading a text document with the submitted information, however I am trying to develop a html form which, when the 'submit' button is pressed, saves the text in the text field(s) as a single text document. 我发现了许多脚本,它们可以将表单输入保存到本地计算机,即IE下载包含提交信息的文本文档,但是我正在尝试开发一个HTML表单,当按下“提交”按钮时,该表单可以保存文本在文本字段中作为单个文本文档。 The closest I've come is the following (Sorry, can't find where I got this code from originally. Hope that's okay :/) 我最接近的是以下内容(很抱歉,无法找到我最初从何处获得此代码。希望没关系:/)

<html>
<head>
<script>
$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 &');
}
</script>
</head>

<body>
<form id="some" name="someName" method="post" action="/../PastEntries/file.txt">
<input type="text" id="some1" class="someClass" value="" name="fileWrite"/>
<input type="submit" value="submit" class="submitClass"/>
</form>
</body>
</html>

However, I get the error "Cannot POST /../www/file.txt" 但是,我收到错误消息“无法发布/../www/file.txt”
I'm new to PHP, and have no idea how to fix this. 我是PHP新手,不知道如何解决此问题。 I hope someone more experienced can help or provide an alternative! 希望更有经验的人可以提供帮助或替代方案!
Thanks in advance! 提前致谢!

Not tested but you could do something like this. 未经测试,但您可以执行类似的操作。 The form action is removed entirely so the form POSTS to the same page - the request is interpreted by PHP and the data written to a text file. 表单动作将被完全删除,因此表单会在同一页面上发布-请求由PHP解释,并将数据写入文本文件。

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        $filename=__DIR__ . '\\file.txt';
        file_put_contents( $filename, implode( PHP_EOL,$_POST ) . PHP_EOL, FILE_APPEND );
        exit();
    }
?>
<!doctype html>
<html>
    <head>
        <title>Save Form data to text file</title>
    </head>
    <body>
        <form id="some" name="someName" method="post">
            <input type="text" id="some1" class="someClass" value="" name="fileWrite"/>
            <input type="submit" value="submit" class="submitClass"/>
        </form>
    </body>
</html>

I had was fiddling around with your code to see if i can make it work. 我一直在摆弄你的代码,看看我是否可以使它工作。 Below is the fixed version. 下面是固定版本。

<html>
<head>

<?php

$fileWrite = '';    
$myFile = "testFile.txt";
if(isset($_POST['fileWrite']) && !empty($_POST['fileWrite'])) {
$fileWrite = $_POST['fileWrite'].PHP_EOL;
}
if($fileWrite) {
$fh = fopen($myFile, 'a') or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
}
?>

</head>

<body>
<form id="some" name="someName" method="post">
<input type="text" id="some1" class="someClass" value="" name="fileWrite"/>
<input type="submit" value="submit" class="submitClass"/>
</form>
</body>
</html>

I have worked around what @RamRider had pointed out earlier and added .PHP_EOL that enables new line break ( check manual for better understanding ). 我已经解决了@RamRider先前指出的问题,并添加了.PHP_EOL来启用新的换行符( 请查阅手册以获得更好的理解 )。 Use this code and let us know if there is any errors. 使用此代码,让我们知道是否有任何错误。 I will try to help as much as i can. 我会尽我所能。

Cheers 干杯

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

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