简体   繁体   English

从表单获取用户输入,使用php写入文本文件

[英]Get user input from form, write to text file using php

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是一个双选项单选按钮,其中“是”和“否”为值。

$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. 再次感谢所有的帮助。

// 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 在impode中用\\ t替换标签

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);
// 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()函数重定向浏览器,可以避免用户重新加载页面并重新提交数据时出现问题。

This is the best example with fwrite() you can use only 3 parameters at most but by appending a "." 这是fwrite()最好的例子,你最多只能使用3个参数,但是附加一个“。” you can use as much variables as you want. 您可以根据需要使用尽可能多的变量。

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);
}

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. 非常非常简单:表单数据将被收集并存储在$ var中,$ var将被写入filename.txt \\ n将添加一个新行。 File Append Disallows to overwrite the file 文件追加禁止覆盖文件

<?php
$var = $_POST['fieldname'];
file_put_contents("filename.txt", $var . "\n", FILE_APPEND);
exit();
?>

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. 只需将表单包装在“if(!isset($ _ POST ['Fname']))”条件中。 Put whatever content you want to show after the form has been posted inside the "else{}" part. 在将表单发布到“else {}”部分之后,放置要显示的任何内容。 So, if the form is posted, the content in the "else" clause will be shown; 因此,如果表单已过帐,则会显示“else”子句中的内容; if the form isn't posted, the content of the "if (!isset($_POST['Fname']))", which is the form itself will be shown. 如果表单未发布,将显示“if(!isset($ _ POST ['Fname']))”的内容,即表单本身。 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. 要在文本文件中写入POSTed值,只需按照上面提到的其他方法。

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

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