PHP post multi forms into one file

I have multiply signup forms across my website that POST to a php file(code below) then are saved to a txt file. I would like if I could get all the forms to save to the same/one txt file

PHP:
<?php

$firstname = @$_POST[‘firstname’];
$lastname = @$_POST[‘lastname’];
$email = @$_POST[‘email’];
$phone = @$_POST[‘phone’];
$month = @$_POST[‘month’];
$zipcode = @$_POST[‘zipcode’];
$zipcode2 = @$_POST[‘other’];
$data = " $firstname $lastname $email $month $zipcode2 $zipcode $phone
";
$filename = fopen(“homepagenewsletter.txt”, “a”);
fwrite($filename, $data);
fclose($filename);
?>

Thank you!!

Provided you open the same file in append mode each time, then yes, you can have as many forms across your site as you like all saving data into a single text file. You may want to separate the values on each line by a comma though (since people may enter phone numbers with spaces in between). I’d also validate the data being submitted before just blindly entering it into your text file (since at the moment, empty forms can be submitted along with newline characters that will wreck the file formatting).

I have about three directories that have signup forms that save into it’s own .txt file in each directory. Since I manually have to go through that data it would be easier if I could access it from one .txt file. I have tried changing the mode for fopen()

Here is an example of how the information submited is saved to the .txt file.
John Doe johntest@doetest.com January 12345
Jane Doe janetest@doetest.com March 12345

You’ll want to change the path of fopen() for each form to point to a single file too. If you’re having trouble doing this, then post your directory setup here along with the location of where those three forms reside.

Right, but what if someone enters a space? Your formatting would become messed up. Like I said in my original post, I’d personally separate the values by commas and write that to a file - this will form a comma-separated values (CSV) file. PHP has a built-in function for this already to make things easier for you, and you can make use of it by simply changing these lines:


// ...
$data = " $firstname $lastname $email $month $zipcode2 $zipcode $phone\
";
// ...
fwrite($filename, $data);
// ...

to these lines:


// ...
$data = array($firstname, $lastname, $email, $month, $zipcode2, $zipcode, $phone);
// ...
fputcsv($filename, $data);
// ...

So all we’ve done is put the data to be written in an array, and passed that to the fputcsv() method.

Here is my directory set up:

-Counter Sign Up
-index.html
-save.php
-countersignup.txt
-Newsletter
-index.html
-save.php
-npgrnewsletter.txt
-Newsletter Home
-index.html
-save.php
-homepagenewsletter.txt

Each .php file saves to the .txt file in that directory.

                I greatly appreciate it!

So how are you distinguishing the data between each of the three forms? If the data for all three forms are being saved to a single text file, then how will you know which data in the text file belongs to which form?

In any case, to get all three forms to save to a single text file, place a text file outside of all three sub directories:


domain.com
global.txt
-Counter Sign Up
     -index.html
     -save.php
-Newsletter
     -index.html
     -save.php
-Newsletter Home  
    -index.html
     -save.php

and change the file path in all three fopen() functions for each form to:


'../global.txt'

What I did was at the beginning of the format to save the data I simply add the forms name. It looks like this for one of the forms:

$data = array( Birthday Club $firstname, $lastname, $email, $month, $zipcode2, $zipcode, $phone);

The end result for:

Birthday Club John Doe johntest@doetest.com January 12345
Survey Jane Doe janetest@doetest.com (123) 456-7890 March 12345
Birthday Club Jimmy Doe jimmytest@doetxt.ca July 36519
Birthday Club Jimmy Bob bobj@doetest.com May 56324
Survey Bob James jamesb@doetexts.org (987) 654-3210


'../global.txt'

Does the …/ direct it back to the main directory?

I changed the fopen() function for one of the forms and it works. I’ll finish changing the rest this evening.

                  Thank you so much! (I have been looking for this solution for several years)

I’d advise you to do it like so instead:


$data = array('Birthday Club', $firstname, $lastname, $email, $month, $zipcode2, $zipcode, $phone);

That way, you can get the form type of the data inserted from the first element of the data array (once processed from the text file).

Yes, the double period (…) goes to the previous level of the directory tree, where your text file should be located for all three forms.


$data = array('Birthday Club', $firstname, $lastname, $email, $month, $zipcode2, $zipcode, $phone);

I will try the apostrophe.

While we are on the subject of php fopen(), I just thought of it…
I have 2 domains
x.com( main site- which we have been talking about)
y.com

How can I save data of a form from site y.com to save it in a directory in a .txt file on x.com?

Thank you for all of your help and differ

Have you considered having the signup data stored in a database table if the site has a database?

Do you mean an option/service through the back end of the host?

I don’t believe fopen() can modify files on remote websites. You could, however, use CURL to send an HTTP POST request from site y.com (on form submission) to site x.com, where site x.com saves this incoming POST data to the text file specified. You will need to verify that this POST data is coming from a known source (perhaps by sending a key along with the POST data to verify the sender as known).

He means persisting the form data in a DBMS (like MySQL), rather than using text files to store the data.


 &lt;form name="Mail_list" action="http://x.com.com/save.php" method="post" 

I changed the post action to a url to site x.com. The data now gets saved on x.com and then redirects back to y.com
That was a great idea, Thank you!