Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created September 30, 2011 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walterdavis/1253946 to your computer and use it in GitHub Desktop.
Save walterdavis/1253946 to your computer and use it in GitHub Desktop.
Accept a form input, and save to a data file on the server.
<?php
//form hamdler to save to CSV
//un-comment the next two lines to troubleshoot
//ini_set('display_errors',1);
//error_reporting(E_ALL);
//MAIN CONFIGURATION
//where the data will be saved -- outside of the Web server's root folder
//this folder 'csv' must exist, and must be set to allow the server to
//write to it. ask your hosting provider if you're unsure.
$data_file = dirname(dirname(__FILE__)) . '/csv/report.csv';
$thanks_page = 'http://www.example.com/index.html';
//LIBRARY CODE, DON'T EDIT
function clean($mxdInput){
//recursive function for multidimensional arrays
if(is_string($mxdInput)) return trim(strip_tags($mxdInput));
$out = array();
foreach($mxdInput as $k=>$v){
$out[$k] = clean($v);
}
return $out;
}
function humanize($strFieldName){
return ucwords(str_replace('_',' ',$strFieldName));
}
$errors = $values = array();
if(isset($_POST['_submit'])){
$fields = clean($_POST);
$headers = array();
foreach($fields as $header => $value){
//add the data only if it's not "private"
if(substr($k,0,1) != '_'){
$headers[] = '"' . addslashes(humanize($header)) . '"';
$values[] = '"' . addslashes($value) . '"';
}
}
if(!file_exists($data_file)){
file_put_contents($data_file,implode(',',$headers) . "\n");
}
file_put_contents($data_file, implode(',',$values) . "\n", FILE_APPEND);
header('Location: ' . $thanks_page);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment