Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Save PHP Form to Excel

Save PHP Form to Excel

Save PHP Form to Excel

Ok I will keep this short and sweet. I am trying to create a PHP UserForm, that will open an existing Excel Workbook (on our server) and save the input from the form to it. The user never needs to see the Excel sheet. I have the html part of the form created but that's it. I'm not sure how to do the rest. Can someone please point me in the right direction? Thanks.

RE: Save PHP Form to Excel

are you sure that's a good idea? storing structured data in a spreadsheet does not seem optimal.  perhaps it would be better to store the data in a database and then attach to the database via the spreadsheet?

anyway, if you are set on doing this then i'd advise not keeping the data in XLS format but in csv.  that is easily written/read in php.  

If it _must_ be in xls(x) format then i'd investigate the excel writer and excel reader packages within pear.  



RE: Save PHP Form to Excel

I would have to second the advise of jpadie.

Data should be stored in a database not a spreadsheet (spreadsheets often get miss-used like this because they are quick & simple to set-up but are very inefficient when they become large & a security hole for sensitive data)

if it is not important enough to store if a proper database then it is better to use a CSV file rather than a proprietary format, it will make it easier to process the data in other applications should the need arise.


I do not Have A.D.D. im just easily, Hey look a Squirrel!

RE: Save PHP Form to Excel

No, I'm not deadset on it being stored directly to the Excel sheet, just as long as it can be pulled up in Excel when the bosses need it. I don't know how to save it to a database either, so can either of you provide some assistance?

RE: Save PHP Form to Excel

Sounds like you are in over your head.

To keep it easy for both you and the Excell happy bosses I would use a CSV format as well as mentioned by jpadie and IPGuru.

The easiest way to do this, is to simply create a file using fopen, and write to it your comma separated strings using fwrite or even fputcsv().

Take a look at the PHP online manual:

If you need more help, we need more specific details of what you want to store, and how your Excel file should look.  


Phil AKA Vacunita
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.

RE: Save PHP Form to Excel

not tested but this might get you started.  key thing is to ensure that each field in your form is listed (case sensitively) in the $fields array at the top of the script.

the field names MUST match the form names (case sensitive again).

to trigger a download point your browser at the script and add



here is the code


$fields = array('name','emailaddress','message');
$host = 'localhost';
$dbname = 'mydb';
$user = 'myuser';
$pwd = 'mypwd';

if (isset($_REQUEST['action'])):
    switch (strtolower($_REQUEST['action'])):
        case 'save':
        case 'download':

function connectdb(){
    global $host, $dbname, $user, $pwd;
    global $pdo;
    $pdo = new PDO(
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
function savedata(){
    global $fields;    
    foreach($fields as $field):
        $placeholder[] = '?';
        $params[] = empty($_REQUEST[$field]) ? '' : trim($_REQUEST[$field];
    global $pdo;
    $s = $pdo->prepare("Insert into formdata (". implode(',', $fields) . ") VALUES (". implode(',',$placeholder) .")");
    return true;

function showthankyoupage(){
    include 'thankyoupagetemplate.php';

function downloaddata(){
    global $fields;
    global $pdo;
    $s = $pdo->prepare("select * from formdata");
    $output = <<<OP
    $output .= implode('</th><th>', $fields);
    $output .= '</th></tr></thead>';
    $output .= '<tbody>';
    while ($row = $s->fetchObject()):
        $inner = array();
        foreach($fields as $field):
            $inner[] = '<td>' . htmlspecialchars($row->$field) . '</td>';
        $output .= '<tr>' . implode("\n", $inner) . '</tr>';
    $output .= "</tbody></thead>";
    header("Content-Disposition: attachment; filename=\"formData.xls\"");
    header("Content-Type: application/vnd.ms-excel");
    echo $output;

function displayform(){
    include 'formpage.php';

RE: Save PHP Form to Excel

Excel is used for some interesting things these days not just simple spreadsheets.
Have a look at http://phpexcel.codeplex.com/ which might be of interest.

RE: Save PHP Form to Excel

Why Excel? And what about Linux users?

http://phpforms.net/ - online form builder

RE: Save PHP Form to Excel

The MS$ class should work on Linux as it's all writen in PHP and creates the XML format rather than the older binary format

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close