Page 1 of 1

web form data to csv file problem

Posted: Fri Apr 22, 2011 8:08 am
by ams007
Hello,

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:

Name,Email
Susan Que,suzyq@gmail.comnAl Sata,al@alsata.comnJohn Public,jp@gmail.comn

instead of this:

Name,Email
Susan Que,suzyq@gmail.com
Al Sata,al@alsata.com
John Public,jp@gmail.com

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 “<h1>You data has been saved in a text file!</h1>”;
?>

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

Any help would be appreciated.

Thanks,
Al

Re: web form data to csv file problem

Posted: Fri Apr 22, 2011 8:32 am
by ams007
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.

Thanks,
Al

Re: web form data to csv file problem

Posted: Fri Apr 22, 2011 12:09 pm
by ayazhaider
Try Following
@yAz

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


//Send Current data in email
// Sending Same CSV file via email just waste of bandwith

$to = 'you@yourdomain.com'; // Write Your email address
$from = 'visitor@yourdomain.com'; // Use an email address for filtering
$headers = 'From: '.$from.'' . '\r\n' .
'Reply-To: '.$from.'' . '\r\n' .
'X-Mailer: PHP/' . phpversion();

$subject = $name.'Add him self on your webform';
$body = 'you have a new visitor on your website: \n
------------------------------------------------- \n
Name : '.$name.' \n
Email : '.$email.' \n
\n For Complete list of registered users Click here http://www.yourdomaiin.com/webform/your_file.txt
';
if (mail($to, $subject, $body, $headers)) {
echo' <h1>Your data has been send and saved in a text file!</h1>';
} else {
echo' Email Sending Failed!! ';
}



?>

Re: web form data to csv file problem

Posted: Fri Apr 22, 2011 1:30 pm
by Celauran
ams007 wrote:An "n" appears after the email address in the csv file instead of a new line being started.

Code: Select all

$savestring = $name . “,” . $email . “n”;
Because that's what you told it to do.

Newline character is \n

Re: web form data to csv file problem

Posted: Thu Apr 28, 2011 2:58 am
by Deforest
Well, try to search in online by using some search engine like google. I think you can be found some info that you can use in your topic. There’s lot of info that you can use in your topic.