Jump to content


Photo

Simple Script to Process Form Data and export to CSV


  • Please log in to reply
2 replies to this topic

#1 ovidx

ovidx
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 05 May 2006 - 07:28 PM

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?


#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 May 2006 - 07:39 PM

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.
    $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>";

Info: PHP Manual


#3 ovidx

ovidx
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 05 May 2006 - 08:03 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users