web form data to csv file problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ams007
Forum Newbie
Posts: 2
Joined: Fri Apr 22, 2011 7:32 am

web form data to csv file problem

Post 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
ams007
Forum Newbie
Posts: 2
Joined: Fri Apr 22, 2011 7:32 am

Re: web form data to csv file problem

Post 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
ayazhaider
Forum Newbie
Posts: 5
Joined: Fri Apr 22, 2011 11:18 am

Re: web form data to csv file problem

Post 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!! ';
}



?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: web form data to csv file problem

Post 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
Deforest
Forum Newbie
Posts: 2
Joined: Thu Apr 28, 2011 2:55 am

Re: web form data to csv file problem

Post 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.
Post Reply