Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Web Design & Development Web Design & Development
Search Search
Search for:
Tech Support Guy > > >

Convert Form Information To Microsoft Excel or CSV File


(!)

hicksjt's Avatar
hicksjt hicksjt is offline
Senior Member with 115 posts.
THREAD STARTER
 
Join Date: Jul 2005
24-Mar-2008, 08:58 AM #1
Convert Form Information To Microsoft Excel or CSV File
Hello,

Is there a way using PHP to create a Microsoft Excel or CSV file on the fly once a user submits information from an online form and then sends it as an attachment via email?

As of now when the form is submitted it goes into a blank page. Thank you for any feedback.

Here is the current PHP code:

<?php

$cr = "\n";
$data = "Name" . ',' . "Email" . ',' . "Age" . ',' "Location". $cr;
$data .= "$name" . ',' . "$email" . ',' . "$age" . ',' "$location". $cr;

$fp = fopen($filename,"a"); // $fp is now the file pointer to file $filename

if($fp)
{
fwrite($fp,$data); // write information to the file
fclose($fp); //close the file
echo "File Saved Successfully"
}
else
{
echo "Error Saving File";
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=test.csv");
header("Pragma: no-cache");
header("Expires: 0");

echo $data;

?>
TheRobatron's Avatar
TheRobatron TheRobatron is offline
Computer Specs
Senior Member with 551 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
24-Mar-2008, 09:28 AM #2
hicksjt's Avatar
hicksjt hicksjt is offline
Senior Member with 115 posts.
THREAD STARTER
 
Join Date: Jul 2005
24-Mar-2008, 09:48 AM #3
Quote:
Originally Posted by TheRobatron View Post
I appreciate the link TheRobatron

From the code description:

"the file has to be uploaded first or should be an existing file on the server"

I hope I'm reading it correctly I believe this would work BUT the data from when a user submits their information into the form would actually create the file, put it as an attachment, and then sent it do the appropriate recipient. Storing the files each time a form is submitted might be a little to much
tomdkat's Avatar
Computer Specs
Trusted Advisor with 7,141 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
24-Mar-2008, 12:40 PM #4
That PHP code snipet looks like it should create the CSV file from the form fields ok. I'm not the "PHP guy" but your code looks fine, for creating the file.

All you need to do now is add code to have it send mail to the appropriate e-mail address. You should be able to look at the PHP source for just about any free PHP form processing script to get the code to e-mail the file that was created to the appropriate address.

Peace...
Sequal7's Avatar
Computer Specs
Senior Member with 2,382 posts.
 
Join Date: Apr 2001
Location: Around the corner!
Experience: Including today?
24-Mar-2008, 01:21 PM #5
Quote:
Originally Posted by tomdkat View Post
All you need to do now is add code to have it send mail to the appropriate e-mail address. You should be able to look at the PHP source for just about any free PHP form processing script to get the code to e-mail the file that was created to the appropriate address.
Just a thought, wouldn't the formail.php script that you are helping aconite work for this too?

http://www.tectite.com/
tomdkat's Avatar
Computer Specs
Trusted Advisor with 7,141 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
24-Mar-2008, 01:27 PM #6
Quote:
Originally Posted by Sequal7 View Post
Just a thought, wouldn't the formail.php script that you are helping aconite work for this too?

http://www.tectite.com/
Not really since the goal isn't to upload a file.

hicksjt wants to have the fields of a HTML form be written out to a CSV file on the server (as part of the form processing) and then have that CSV file e-mailed to someone.

It looks like the PHP to generate the CSV file is ok and all hicksjt needs now is the PHP to attach that file to an e-mail message that gets sent out.

For a standard file upload situation, I would recommend FormMail with a smile.

Peace...
TheRobatron's Avatar
TheRobatron TheRobatron is offline
Computer Specs
Senior Member with 551 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
24-Mar-2008, 01:45 PM #7
You could use the unlink function (see the PHP manual at http://us3.php.net/manual/en/function.unlink.php) to delete the file as soon as you've sent it. I think you will have to create a local copy of the file in order to send it as an attachment.
tomdkat's Avatar
Computer Specs
Trusted Advisor with 7,141 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
24-Mar-2008, 01:59 PM #8
Quote:
Originally Posted by TheRobatron View Post
You could use the unlink function (see the PHP manual at http://us3.php.net/manual/en/function.unlink.php) to delete the file as soon as you've sent it.
Agreed.

Quote:
I think you will have to create a local copy of the file in order to send it as an attachment.
Why? The file would already be on the server and the e-mail would be sent from the server, just as if the file had been uploaded to the server.

Peace...
italiangar's Avatar
italiangar italiangar is offline
Computer Specs
Junior Member with 1 posts.
 
Join Date: May 2008
Experience: Intermediate
18-May-2008, 07:36 AM #9
Is this solved?
I am interested in doing the same thing - ie taking form fields submitted by people and outputting them as csv or excel compatible.

Many thanks!
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑