# Convert Form Information To Microsoft Excel or CSV File



## hicksjt (Jul 21, 2005)

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 (Oct 25, 2007)

Is this what you want?

http://www.finalwebsites.com/snippets.php?id=36


----------



## hicksjt (Jul 21, 2005)

TheRobatron said:


> Is this what you want?
> 
> http://www.finalwebsites.com/snippets.php?id=36


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 (May 6, 2006)

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 (Apr 15, 2001)

tomdkat said:


> 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 (May 6, 2006)

Sequal7 said:


> 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 (Oct 25, 2007)

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 (May 6, 2006)

TheRobatron said:


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



> 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 (May 18, 2008)

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!


----------

