October 22, 2004 1:05 AM   Subscribe

HTML form to CSV file: Simple enough for a remedial programmer, but difficult for an artsy designer. Any advice? [mi]

I'm looking for a script - either in PHP or CGI - that I can use to have form data dumped to a CSV file. Each time the form is filled out and submitted, a new row gets added to the file. After a time, I can download the file, open it up in Excel (or what have you) and analyze the data.

I'm certain there are publicly available scripts out there, but my Google isn't coming through for me. I'm not sure I know the proper vernacular for the search.

Thus, any advice from you bad ass code people would be very welcome. Thanks!
posted by aladfar to Computers & Internet (6 answers total)

$fieldnames = array('firstfieldname','secondfieldname','etc');
$values = array();

foreach($fieldnames as $fieldname)

$csvrow = join(', ',$values);

if($fptr = fopen('a','filename'))

posted by weston at 1:55 AM on October 22, 2004

TFmail does this. Although it's primarily about form-to-email, it will also write the form input to a csv file. I made it work and I no habla php.
posted by stupidsexyFlanders at 6:03 AM on October 22, 2004

I can't change your code because i'm writing this on a PDA but

1. You need to lock the file before writing otherwise data *may* get lost.
2. If someone enters comma's and/or speech marks then your CSV will break. You need to escape these properly.

I don't know if there's a PHP function to do #2.
posted by ralawrence at 6:07 AM on October 22, 2004

I made it work and I no habla php.

I'll correct myself, TFmail is perl, not php. Sorry.

I also no habla perl.
posted by stupidsexyFlanders at 7:43 AM on October 22, 2004

ralawrence is correct... in this case, unless you're really attached to csv as a format, I recommend tab delimited files. They're easier to read for a person and just as easily read into spreadsheets. And you can't actually type a tab into fields on most web browsers, even if people did in fact ever use them in that context. Changing the join line to join("\t",$values) would do this.

File locking: yeah. This is a problem, however, unless your site is hight traffic the likelihood of seeing this problem is low. Usually by the time I get to locking-level concerns, that's a signal to me that I probably should be considering using a database. Hence my habit of not writing locking into solutions...
posted by weston at 3:09 PM on October 22, 2004

Thanks very much! I'm going to poke around with TFmail and the supplied script. I think I'll be able to take it from here though.
posted by aladfar at 6:24 PM on October 22, 2004

« Older Music Sales Figures   |   Cascading CSS Newer »
This thread is closed to new comments.