I was thinking that the $fields array needs to be populated, but eForm has to be faked into believing that it’s reloading the form after a failed validation, only without the error messages.
/* function 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'];
$_SESSION['prepopulate']='yes';
$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'] = $_POST['email'] = strtolower($_SESSION['startemail']);
$query = "SELECT * FROM modx_applications WHERE email='".$modx->db->escape($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='".$modx->db->escape($fields['email'])."', status='unfinished', started=NOW(), modified=NOW()";
$result = $modx->dbQuery($query);
$fields['appid'] = $_POST['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] = $_POST[$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']='';
$_POST['formid']='application';
}
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']);
foreach($fields as $key=>$value){
$fields[$key] = str_replace('"', '"', $value);
}
if ($_SESSION['prepopulate']=='yes'){
$fields['validationmessage']='';
$vMsg=array(' ');
$rMsg=array();
$_lang["ef_validation_message"]='';
$_SESSION['prepopulate']='no';
}
elseif ($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.'=\''.$modx->db->escape($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.'=\''.$modx->db->escape($value).'\', ';
// echo '<p>'.$key.' - '.$value.'</p>';
}
}
$query .= 'status=\'complete\', ';
$query .= ' modified=NOW()';
$query .= ' WHERE appid=\''.$fields['appid'].'\'';
$result = $modx->dbQuery($query);
return true;
}
/* minor change to eform.inc.php at line # 141 (eForm 1.4.1) */
if ($isPostBack || $_SESSION['prepopulate']=='yes') {
if StepCompleted = '2'
then header: location 'step3-page',
else if StepCompleted = '1'
then header: location 'step2-page',
else
header: location 'step1-page'
but in practice I can’t get eForm to treat a fresh form as if it’s been posted (perhaps I need to refresh the page with the post values?)...
Quote from: ZAP at Dec 06, 2006, 09:24 AM
but in practice I can’t get eForm to treat a fresh form as if it’s been posted (perhaps I need to refresh the page with the post values?)...
i didnt understand this.
Chuck
@ZAP, I haven’t been following this thread but have you seen topic? with an example for multi page forms?
The form doesn’t populate (show values) unless eForm can be made to think that it has been posted. This is because eForm is now super-cool in that it adds value="[+placeholder+]" to each form field (as well as selecting and checking the appropriate ones), but only AFTER the form has been posted. So even if you set the $fields or $_POST values in a function triggered by an event that happens before the form is parsed, these values are not inserted into the form.
So there needs to be some other trigger to force eForm to parse the form without a post.