Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

laurajones (Programmer)
18 Aug 11 10:11
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.
Helpful Member!  jpadie (TechnicalUser)
18 Aug 11 10:55
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.  

http://sourceforge.net/projects/phpexcelreader/
http://pear.php.net/package/Spreadsheet_Excel_Writer

 
IPGuru (TechnicalUser)
18 Aug 11 11:02
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!

laurajones (Programmer)
18 Aug 11 11:27
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?
vacunita (Programmer)
18 Aug 11 11:37
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:
http://php.net/manual/en/function.fputcsv.php
http://php.net/manual/en/function.fopen.php
http://php.net/manual/en/function.fwrite.php\

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.
http://behindtheweb.blogspot.com/
 

jpadie (TechnicalUser)
18 Aug 11 13:12
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

CODE

&action=download

here is the code

CODE


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

if (isset($_REQUEST['action'])):
    switch (strtolower($_REQUEST['action'])):
        case 'save':
            savedata();
            showthankyoupage();
            break;
        case 'download':
            downloaddata();
            break;
    endswitch;    
else:
    displayform();
endif;

function connectdb(){
    global $host, $dbname, $user, $pwd;
    global $pdo;
    $pdo = new PDO(
        "mysql:host=$hose;dbname=$dbname",
        $user,
        $pwd
        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];
    endforeach;
    connectdb();
    global $pdo;
    $s = $pdo->prepare("Insert into formdata (". implode(',', $fields) . ") VALUES (". implode(',',$placeholder) .")");
    $pdo->execute($params);
    return true;
}

function showthankyoupage(){
    ob_end_clean();
    include 'thankyoupagetemplate.php';
    exit;
}

function downloaddata(){
    global $fields;
    connectdb();
    global $pdo;
    $s = $pdo->prepare("select * from formdata");
    $s->execute();
    $output = <<<OP
<table>
    <thead>
        <tr>
            <th>
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>';
        endforeach;
        $output .= '<tr>' . implode("\n", $inner) . '</tr>';
    endwhile;
    $output .= "</tbody></thead>";
    ob_end_clean();
    header("Content-Disposition: attachment; filename=\"formData.xls\"");
    header("Content-Type: application/vnd.ms-excel");
    echo $output;
}

function displayform(){
    include 'formpage.php';
}
?>
ingresman (IS/IT--Management)
24 Aug 11 9:56
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.
stentorian (IS/IT--Management)
29 Aug 11 10:54
Why Excel? And what about Linux users?

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

ingresman (IS/IT--Management)
30 Aug 11 16:08
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

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!

Back To Forum

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