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

31,815

Solution 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

Solution 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);
Share:
31,815
Admin
Author by

Admin

Updated on July 13, 2022

Comments

  • Admin
    Admin almost 2 years

    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.