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

浪子不回头ぞ 提交于 2019-12-21 05:13:06

问题


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.

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.

This will be on a wordpress blog and contained within a popup.

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.

$country is a drop down with 40 or so countries.

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?

Thanks again for all the help.


回答1:


// 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




回答2:


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



回答3:


// 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.




回答4:


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.




回答5:


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.

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



回答6:


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

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


来源:https://stackoverflow.com/questions/2009631/get-user-input-from-form-write-to-text-file-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!