1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Convert Form Information To Microsoft Excel or CSV File

Discussion in 'Web Design & Development' started by hicksjt, Mar 24, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. hicksjt

    hicksjt Thread Starter

    Joined:
    Jul 21, 2005
    Messages:
    115
    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;

    ?>
     
  2. TheRobatron

    TheRobatron

    Joined:
    Oct 25, 2007
    Messages:
    551
  3. hicksjt

    hicksjt Thread Starter

    Joined:
    Jul 21, 2005
    Messages:
    115
    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 :)
     
  4. tomdkat

    tomdkat Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,141
    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...
     
  5. Sequal7

    Sequal7

    Joined:
    Apr 14, 2001
    Messages:
    2,382
    Just a thought, wouldn't the formail.php script that you are helping aconite work for this too?

    http://www.tectite.com/
     
  6. tomdkat

    tomdkat Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,141
    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...
     
  7. TheRobatron

    TheRobatron

    Joined:
    Oct 25, 2007
    Messages:
    551
    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.
     
  8. tomdkat

    tomdkat Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,141
    Agreed.

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

    italiangar

    Joined:
    May 18, 2008
    Messages:
    1
    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!
    :)
     
  10. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/696520