I'm trying to make a small change to the script to give it database functionality. I essentially want it to write the data of the form to a mysql database after it has confirmed and e-mailed the info.
I'm having trouble primarily because I'm not sure where to include my code within the existing code without getting errors.
Can anyone be of any assistance? I'd be happy to post my code if there is any interest.
modification
I would recomend putting your database code inside the send_mail() function.
-Andrew Riley
modification
The following changes will allow the form field data to be stored in a flat text file:
After $invis_array definition insert the following:
////// Database - write CSV file with data of submitted forms //////////////
// Fields to collect
// $database_fields = '*' - mean all fields, as in form
// $database_fields = array('from', 'subject') - only 'from', 'subject' fields
$database_enabled = 1; //0 if not enabled
$database_file = 'data/email.csv'; //name of file where data is to be appended, will create if not already in existance
$database_fields = array('fielda','fieldb','fieldc','etc','etc');
// This line defines the order in which the fields should be written
Then add the following function
function save_form($vars){
global $database_file, $database_fields;
$f = fopen($database_file, 'a');
if (!$f){die("Cannot open db file for save");}
foreach ($vars as $k=>$v) {$vars[$k] = str_replace(array("|", "\r","\n"), array('_',' ',' '), $v);}
if (is_array($database_fields)) {
$vars_orig = $vars;
$vars = array();
foreach ($database_fields as $k)
$vars[$k] = $vars_orig[$k];
}
$str = join('|', $vars);
fwrite($f, $str."\n");
fclose($f);
}
and the following line
save_form($form);
after
$form = decode_vars();
if (count($form) > 0) {
Can't claim much credit for this as I took it from aformmail from cgi-central-net
modification question
Paul,
I'm using that piece as well, but I'm having some trouble. The results are not listing all fields, so my results are ending up in the wrong columns.
Any idea how to list the blank fields as well? Bear in mind I'm a non-techie, so write slow so even a doofus like me would get it.
Incidentally, I have no idea if this matters, but I have two checkboxes on the form - users are supposed to check one or the other. I'd like it so the checked box gets a "1" and the other a "0", or some other way of distinguishing which of the two is checked.
Many thousand thanks in advance,
Dan
modification
Did you try setting the parameter "print_blank_fields" in your form?
modification
mine doesn't want to create the text file, the form still works and send me an email, but no flat file to be found... could it have something to do with my php settings? I set fields to *
modification
Would it not be just as easy to specify an action to your form as 'formmail.php'
That way when you press submit, your form has to process your form and submit your data to be processed for the email to be sent.
At the same time, the submit button will also allow any database inserts to be actioned.
I've done something similar over a multi page form and it works fine :D
Adding form to database
I have added the code suggested above and it all seems to be in the right places. However, when I try to send from the form I get the error message
"Cannot open db file for save"
do I need to create the database file before using the script?
modification
Just for the record, I have now got this working.
Had to create the email.csv file and upload to the server before running the script but it now works perfectly.
Many thanks
Mike
CSV output
This added code is creating a file from the submitted form, but the fields are seperated b "|" rather then ",". What should be changed
}
?? $str = join('|', $vars);
fwrite($f, $str."\n");
fclose($f);
}
[/code]