从表单获取用户输入,使用 php 写入文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2009631/
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
Get user input from form, write to text file using php
提问by Joe
As part of a subscriber acquisition I am looking to grab user entered data from a html form and write it to a tab delimited text file using php.The data written needs to be separated by tabs and appended below other data.
作为订阅者获取的一部分,我希望从 html 表单中获取用户输入的数据,并使用 php 将其写入制表符分隔的文本文件。写入的数据需要由制表符分隔并附加在其他数据下方。
After clicking subscribe on the form I would like it to remove the form and display a small message like "thanks for subscribing" in the div.
单击表单上的订阅后,我希望它删除表单并在 div 中显示一条小消息,如“感谢订阅”。
This will be on a wordpress blog and contained within a popup.
这将在 wordpress 博客上并包含在弹出窗口中。
Below are the specific details. Any help is much appreciated.
下面是具体的细节。任何帮助深表感谢。
The Variables/inputs are
变量/输入是
$Fname = $_POST["Fname"];
$email = $_POST["emailPopin"];
$leader = $_POST["radiobuttonTeamLeader"];
$industry = $_POST["industry"];
$country = $_POST["country"];
$zip = $_POST["zip"];
$leader is a two option radio button with 'yes' and 'no' as the values.
$leader 是一个两个选项的单选按钮,其值为“yes”和“no”。
$country is a drop down with 40 or so countries.
$country 下降了 40 个左右的国家。
All other values are text inputs.
所有其他值都是文本输入。
I have all the basic form code done and ready except action, all I really need to know how to do is:
我已经完成了所有基本的表单代码并准备好了,除了行动,我真正需要知道如何做的是:
How to write to a tab delimited text file using php and swap out the form after submitting with a thank you message?
如何使用 php 写入制表符分隔的文本文件并在提交感谢信息后换出表单?
Thanks again for all the help.
再次感谢所有的帮助。
回答by Erik
// the name of the file you're writing to
$myFile = "data.txt";
// opens the file for appending (file must already exist)
$fh = fopen($myFile, 'a');
// Makes a CSV list of your post data
$comma_delmited_list = implode(",", $_POST) . "\n";
// Write to the file
fwrite($fh, $comma_delmited_list);
// You're done
fclose($fh);
replace the , in the impode with \t for tabs
用 \t 替换 , 在 impode 中用于制表符
回答by a.yastreb
Open file in append mode
以追加模式打开文件
$fp = fopen('./myfile.dat', "a+");
And put all your data there, tab separated. Use new line at the end.
并将所有数据放在那里,制表符分隔。最后使用新行。
fwrite($fp, $variable1."\t".$variable2."\t".$variable3."\r\n");
Close your file
关闭您的文件
fclose($fp);
回答by Mark Moline
// format the data
$data = $Fname . "\t" . $email . "\t" . $leader ."\t" . $industry . "\t" . $country . "\t" . $zip;
// write the data to the file
file_put_contents('/path/to/your/file.txt', $data, FILE_APPEND);
// send the user to the new page
header("Location: http://path/to/your/thankyou/page.html");
exit();
By using the header() function to redirect the browser you avoid problems with the user reloading the page and resubmitting their data.
通过使用 header() 函数重定向浏览器,您可以避免用户重新加载页面和重新提交数据的问题。
回答by Kneel-Before-ZOD
To swap out the form is relatively easy. Make sure you set the action of the form to the same page. Just wrap the form inside a "if (!isset($_POST['Fname']))" condition. Put whatever content you want to show after the form has been posted inside the "else{}" part. So, if the form is posted, the content in the "else" clause will be shown; if the form isn't posted, the content of the "if (!isset($_POST['Fname']))", which is the form itself will be shown. You don't need another file to make it work.
To write the POSTed values in a text file, just follow any of the methods other people have mentioned above.
换出表格相对容易。确保将表单的操作设置为同一页面。只需将表单包装在“if (!isset($_POST['Fname']))”条件中即可。将您想在表单发布后显示的任何内容放入“else{}”部分。所以,如果表单被张贴,“else”子句中的内容将被显示;如果表单未发布,则将显示“if (!isset($_POST['Fname']))”的内容,即表单本身。您不需要其他文件即可使其工作。
要将 POSTed 值写入文本文件,只需按照其他人在上面提到的任何方法即可。
回答by shaz3e
This is the best example with fwrite() you can use only 3 parameters at most but by appending a "." you can use as much variables as you want.
这是 fwrite() 的最佳示例,您最多只能使用 3 个参数,但可以附加一个“.”。您可以根据需要使用尽可能多的变量。
if isset($_POST['submit']){
$Fname = $_POST["Fname"];
$email = $_POST["emailPopin"];
$leader = $_POST["radiobuttonTeamLeader"];
$industry = $_POST["industry"];
$country = $_POST["country"];
$zip = $_POST["zip"];
$openFile = fopen("myfile.ext",'a');
$data = "\t"."{$Fname}";
$data .= "\t"."{$email}";
$data .= "\t"."{$leader}";
$data .= "\t"."{$industry}";
$data .= "\t"."{$country}";
$data .= "\t"."{$zip}";
fwrite($openFile,$data);
fclose($openFile);
}
回答by DeathRs
Very very simple:Form data will be collected and stored in $var data in $var will be written to filename.txt \n will add a new line. File Append Disallows to overwrite the file
非常非常简单:表单数据将被收集并存储在 $var 中, $var 中的数据将被写入 filename.txt \n 将添加一个新行。File Append 不允许覆盖文件
<?php
$var = $_POST['fieldname'];
file_put_contents("filename.txt", $var . "\n", FILE_APPEND);
exit();
?>

