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

As part of acquiring a subscriber, I am going to capture user input from an html form and write it to a tab delimited text file using php. Recorded data should be tabbed and added below other data.

After you have subscribed to the form, I would like to delete this form and display a small message like "thank you for subscribing" in the div.

This will be on the Wordpress blog and is contained in a popup.

The following are specific details. Any help is greatly appreciated.

Variables / Inputs

$Fname = $_POST["Fname"]; $email = $_POST["emailPopin"]; $leader = $_POST["radiobuttonTeamLeader"]; $industry = $_POST["industry"]; $country = $_POST["country"]; $zip = $_POST["zip"]; 

$ leader is a two-way switch with yes and no as values.

$ country is a fall with 40 countries.

All other values ​​are text inputs.

I have all the basic form code ready and ready, with the exception of the action, and I really need to know how to do this:

How to write to a tab delimited text file using php and change the form after sending with a thank you note?

Thanks again for the help.

+4
source share
6 answers
 // 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 in impode with \ t for tabs

+9
source

Open file in upload mode

 $fp = fopen('./myfile.dat', "a+"); 

And put all your data there, the tab is split. Use a new line at the end.

 fwrite($fp, $variable1."\t".$variable2."\t".$variable3."\r\n"); 

Close the file

 fclose($fp); 
+3
source
 // 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(); 

Using the header () function to redirect the browser, you avoid problems with reloading the page by the user and re-sending your data.

0
source

Changing the shape is pretty easy. Make sure the form is on the same page. Just wrap the form inside the if (! Isset ($ _ POST ['Fname'])) condition. Place the content that you want to display after the form has been placed inside the "else {}" part. So, if the form is published, the content in the "else" clause will be displayed; if the form is not submitted, the contents of "if (! isset ($ _ POST ['Fname']))", which is the form itself, will be displayed. You do not need another file to make it work.

To write POSTED values ​​in a text file, simply follow any of the methods mentioned above.

0
source

This is the best example with fwrite (), you can use a maximum of 3 parameters, but adding ".". you can use as many 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); } 
0
source

Very simple: Form data will be collected and saved in $ var; data in $ var will be written to filename.txt \ n will add a new line. File Append Prevents file overwriting

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

All Articles