Jump to content

Simple Script to Process Form Data and export to CSV


ovidx

Recommended Posts

I've got a form where users are submitting survey information to me. When the user clicks the "Submit" button, he is taken to a "Thank You" page. I'm looking for a simple PHP script that will...

1.) Process the form data and email me a copy of it (the user's name, email address, and question)
2.) Write the form data to a CSV file.

Since I will have about 200 people responding to the survey, it would be a LOT easier to analyze the data using an Excel spreadsheet. So, looking for something that will write to a CSV file.

Any simple scripts available for this?
The first part you'll have to write yourself... but here's an example of a way to write out a CSV formatted file... some modifications will have to be made. This works with a MSSQL database and spits out the column names and then the data.[code]    $ehandle2 = new sql_handler(1, "", "queryoutput.php");
    $exitcode = 0;
    //echo "<br/>" . stripslashes($query) . "<br/>";
    $result = $ehandle2->update_query(stripslashes($query));
    if(!$result)
        $exitcode++;
        
    if($exitcode == 0)
    {
    //C:\Inetpub\wwwroot\Query\temp
    
    $filelink = "./temp/qry" . date("HismdY") . ".txt";
    $filename = "C:\\Inetpub\\wwwroot\\Query2\\temp\qry" . date("HismdY") . ".txt";
    
    if (!$handle = fopen($filename, "wb"))
    {
        echo "Cannot open file " . $filename;
        exit();
    }
    
    $printcolnames = 1;
    while($row = mssql_fetch_array($result))
    {
        $rowval = "";
        // ============ print column names ====================
        if($printcolnames == 1)
        {
            $rowname = "";
            for($i=0;$i<mssql_num_fields($result);$i++)
                $rowname .= mssql_field_name($result, $i) . "\t";
            $rowname = substr($rowname,0,-1) . "\n";
            if (!fwrite($handle, $rowname))
            {
                echo "Cannot write to file $filename";
                exit;
            }    
            $printcolnames = 0;
        }
        // ========= end print column names ===================
        
        // ========= print col values ========================
        for($i=0;$i<mssql_num_fields($result);$i++)
            $rowval .= $row[$i] . "\t";
                
        $rowval = substr($rowval,0,-1) . "\n";
        
        if (!fwrite($handle, $rowval))
        {
            echo "Cannot write to file $filename";
            exit;
        }    
        // ========= end print col values ===================
    }
    
    fclose($handle);
    
    echo "<br/><br/><p class=pheader2>Your results can be found here: *</p><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - <a href=\"" . $filelink . "\" target=\"_blank\">Tab Delimited Results File</a>&nbsp;&nbsp;&nbsp;<span class=chatter>(right-click -> \"Save Target As ...\" to save to local PC, then open with Excel)</span>";[/code]
Ober - Wow, thanks. Unfortunately, most of what you wrote went straight over my head. I forgot to mention that I'm new to PHP. My knowledge of PHP consists of echo/print commands, and using PHP and ModRewrite to redirect/cloak affiliate links. That's about it, right now. Unfortunately, I don't have the knowledge needed to write a script that will process form data, email me a copy, and export to CSV.

Here's a more basic question, how do I transfer the user to the thank-you page, after he hits the submit button?

I know someone who writes CGI scripts, so I think I'll just email him. I wanted to process the data in PHP, since the pages have PHP extensions and I'm echoing the first name entered on index.php inside the "Thank you/redirect" page. I just thought it would be easier, but I'll see what I can find CGI-wise. Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.