php 如何将在 HTML 表单中输入的字符串保存到文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35443812/
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
How to save string entered in HTML form to text file
提问by HoustoneD
I have a very basic PHP file. i want to have two textboxes for user input, and a submit button. The user will enter their first and last name, then i would like to append or create a TXT file with the data entered from field1 and field2.
我有一个非常基本的 PHP 文件。我想要两个用于用户输入的文本框和一个提交按钮。用户将输入他们的名字和姓氏,然后我想附加或创建一个 TXT 文件,其中包含从 field1 和 field2 输入的数据。
Possibly i am going about this the wrong way. I will post two of the ways i have been tinkering around with.
可能我以错误的方式解决这个问题。我将发布我一直在修补的两种方式。
<html>
<head>
<title>Field1 & 2</title>
</head>
<body>
<form>
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
<?php
$txt= $_POST['field1'].' - '.$_POST['field2'];
$var_str3 = var_export($txt, true); //is this necessary?
$var3 = "$var_str3"; //is this necessary?
file_put_contents('fields.txt', $var3.PHP_EOL, FILE_APPEND);
?>
</body>
</html>
I cant figure out how to get the data from field1 and field2 into a string variable.
我不知道如何将 field1 和 field2 中的数据放入字符串变量中。
I have also messed around with using this php instead of the section listed above
我也搞砸了使用这个 php 而不是上面列出的部分
<?php
$txt= "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) {
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
回答by Prosen Ghosh
You should learn about HTML FormsAnd PHP Form Handling.
In your code you have to use a form HTTP method. And the form data must sent for processing to a PHP file.
在您的代码中,您必须使用表单 HTTP 方法。并且必须将表单数据发送到 PHP 文件进行处理。
In this code i use HTTP PSOT method you can also use GET method the result will be same. This two method is used for collecting the form data. And the php file name is "action.php"
.
在此代码中,我使用 HTTP PSOT 方法,您也可以使用 GET 方法,结果将相同。这两种方法用于收集表单数据。并且 php 文件名是"action.php"
.
index.html
索引.html
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="action.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
action.php
动作.php
<?php
$path = 'data.txt';
if (isset($_POST['field1']) && isset($_POST['field2'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
回答by Paolo
Let's take a snippet from http://www.w3schools.com/html/html_forms.asp
让我们从http://www.w3schools.com/html/html_forms.asp 中提取一个片段
<form action="action_page.php" method="post">
First name:<br>
<input type="text" name="firstname" value=""><br>
Last name:<br>
<input type="text" name="lastname" value=""><br><br>
<input type="submit" value="Submit">
</form>
Note the first line: upon form submission a php script is called: action_page.php
.
请注意第一行:在提交表单时,将调用一个 php 脚本:action_page.php
.
action_page.php
is your webpage with the form and the embedded php script. action_page.php
both displays the empty form and then process the submitted data.
action_page.php
是带有表单和嵌入式 php 脚本的网页。action_page.php
两者都显示空表单,然后处理提交的数据。
On the first line also it is specified that the submitted data is sent with the POST
method.
在第一行还指定提交的数据与POST
方法一起发送。
The php part will look like this:
php 部分将如下所示:
<?php
if( isset($_POST['firstname'] ) && isset( $_POST['lastname'] ) )
{
$txt= $_POST['firstname'].' - '.$_POST['lastname'] . PHP_EOL;
file_put_contents('fields.txt', $txt, FILE_APPEND);
}
?>
The if
statement is there because the first time the script action_page.php
is loaded its purpose is only to display the form and don't receive any POST data.
该if
语句在那里是因为第一次action_page.php
加载脚本时,它的目的只是显示表单而不接收任何 POST 数据。
As the form is submitted by the user the script will receive the data and store to file.
当用户提交表单时,脚本将接收数据并存储到文件中。
The script will also (with this approach) display again an empty form ready for the submission of another entry.
该脚本还将(使用这种方法)再次显示一个空表单,准备提交另一个条目。
You can rearrange things in order to have two web pages: one with just the form, another one with a "Thank you" message and the data processing php script.
您可以重新排列内容以创建两个网页:一个只有表单,另一个带有“谢谢”消息和数据处理 php 脚本。