web form data to csv file problem


I am using a simple snippet of code I found online to take web form data and save it to a csv file. The problem is that the resulting csv file looks like this:

Susan Que,[email protected] Sata,[email protected] Public,[email protected]

instead of this:

Susan Que,[email protected]
Al Sata,[email protected]
John Public,[email protected]

An “n” appears after the email address in the csv file instead of a new line being started. I see the line in the code, but I don’t know why it’s there or if something else should be in it’s place.

This is the code that adds the data to the csv file:

<?php $name = $_POST['name']; $email = $_POST['email']; $fp = fopen(”formdata.txt”, “a”); $savestring = $name . “,” . $email . “n”; fwrite($fp, $savestring); fclose($fp); echo “

You data has been saved in a text file!

”; ?>

Code comes from: http://www.howtoplaza.com/save-web-form-data-text-file

Btw, if someone can also tell me how to have the csv file (formdata.txt) sent to me by email everytime it’s updated with new data, that would be appreciated as well.


You’re missing a backslash from this line:

[php]$savestring = $name . “,” . $email . “n”;[/php]
Should be:

[php]$savestring = $name . “,” . $email . “\n”;[/php]
PHP has some built in functions (fputcsv and fgetcsv) for dealing with CSV which will properly format the data. You should try use them instead.


Thank you, that did it. I am going to read up on the functions that you mentioned.

Btw, is there an easy way to have the csv file emailed to me after every update?

Thanks again,

PHP has a mail() function which you could use.

If you just read the entire CSV file into a string and then passed that as the message to the PHP mail function it should work.

Sponsor our Newsletter | Privacy Policy | Terms of Service