OK. I’m VERY CLOSE to being able to do what I need to do now. Only one detail seems to be eluding me...
I am now able to save unfinished forms to a database table (as well as completed ones). I am using a combination of eForm events to get and save info to a table (code below). However for some reason I haven’t been able to pre-populate the form when people return to finish their application.
What I’m trying to do (because it was the only way that seemed to work) is to use a separate form with only an email field to start off the process. All this form needs to do is to pass the email address to the second form in a session variable (plus a flag that lets the second form know to pre-populate itself). This seems to be working fine (the session vars are set by the first form, and the second form receives and processes the info as expected), but I can’t seem to fill in the form fields with the info that was saved previously.
I would think this would be just a matter of setting the $fields to the value in the database, but it’s just not working for me.
Here is my code:
/* functions called by first form */
// pass entered info to next form
// runs on eFormOnBeforeMailSent event
function passInfo( &$fields ){
global $modx;
$_SESSION['start']='start';
$_SESSION['startemail']=$fields['email'];
$appdocid=434; //id of application page
$apppage= $modx->makeUrl($appdocid);
return $modx->sendRedirect($apppage);
}
/* functions called by second form */
// get saved info from database
// runs on eFormOnBeforeFormMerge event
function populateData( &$fields ){
global $modx;
if ($_SESSION['start']=='start'){
$fields['email'] = strtolower($_SESSION['startemail']);
$query = "SELECT * FROM modx_applications WHERE email='".$fields['email']."' and status='unfinished'";
$result = $modx->dbQuery($query);
if(mysql_num_rows($result)!=1){ // Starting a new application - inserts the data, but doesn't populate the email field in the form
$query = "INSERT INTO modx_applications SET email='".$fields['email']."', status='unfinished', started=NOW(), modified=NOW()";
$result = $modx->dbQuery($query);
$fields['appid'] = mysql_insert_id();
}
else{ // Continuing a saved application
$appInfo=mysql_fetch_array($result, MYSQL_ASSOC);
foreach($appInfo as $key=>$value){
if($key!='status' && $key!='started' && $key!='modified'){
$fields[$key] = $appInfo[$key];
//echo '<p>'.$key.' - '.$value.'</p>'; // I can tell by echoing this data that the database info is being recalled correctly
}
}
}
$_SESSION['start']='';
}
return true;
}
// function to store submitted data in the database
// runs on eformOnValidate event
function updateDatabase( &$fields, &$vMsg, &$rMsg ){
global $modx;
global $_lang;
$fields['email'] = strtolower($fields['email']);
//$fields['save']='save'; // will be set with JavaScript in form
//if($fields['appid']=='') $fields['appid']=$_SESSION['appid'];
if ($fields['save']=='save'){
if ( !preg_match('/^(?:[a-z0-9_-]+?\.)*?[a-z0-9_-]+?@(?:[a-z0-9_-]+?\.)*?[a-z0-9_-]+?\.[a-z0-9]{2,5}$/i', $fields['email']) ) {
$fields['validationmessage']='You must enter a valid email address in order to save your unfinished application. You will need it to retrieve your information later.';
$vMsg=array(' ');
$rMsg=array();
$_lang["ef_validation_message"]='';
}
else{
$query = 'UPDATE modx_applications SET ';
foreach($fields as $key=>$value){
if($key!='formid' && $key!='appid' && $key!='send' && $key!='save' && $key!='savebutton' && $key!='validationmessage'){
$query .= $key.'=\''.$value.'\', ';
// echo '<p>'.$key.' - '.$value.'</p>';
}
}
$query .= 'status=\'unfinished\', ';
$query .= ' modified=NOW()';
$query .= ' WHERE appid=\''.$fields['appid'].'\'';
$result = $modx->dbQuery($query);
$fields['validationmessage']='<h3>Your information has been saved.</h3>';
$vMsg=array(' ');
$rMsg=array();
$_lang["ef_validation_message"]='';
}
}
return true;
}
// saves final info into database
// runs on eFormOnMailSent event
function finalizeDatabase( &$fields ){
global $modx;
$query = 'UPDATE modx_applications SET ';
foreach($fields as $key=>$value){
if($key!='formid' && $key!='appid' && $key!='send' && $key!='save' && $key!='savebutton' && $key!='validationmessage' && $key!='postdate' && $key!='disclaimer' && $key!='subject'){
$query .= $key.'=\''.$value.'\', ';
// echo '<p>'.$key.' - '.$value.'</p>';
}
}
$query .= 'status=\'complete\', ';
$query .= ' modified=NOW()';
$query .= ' WHERE appid=\''.$fields['appid'].'\'';
$result = $modx->dbQuery($query);
return true;
}
What am I missing here? I could set placeholders for most form fields, but that wouldn’t work for checkboxes and such. Plus I know that there must be some minor detail that would fix everything...
Anyone able to help me out?