Forums

Variable to link tables

lcbarker 02 Feb, 2012
I have created several forms which are to write related information into several tables. I would like to have a field in each table that has the same information for set of records created by the multi-page form. Is there a way of setting a variable in the first page that can remain constant throughout the process and can be stored into each of the tables? How would I set that and does it stay active until I finish all the pages of the form?

Thanks

Les
GreyHead 02 Feb, 2012
Hi Les,

I do this by using my Unique ID [GH] action in the OnSubmit event of the first form then saving the resulting ID (a random text sequence) into the Joomla! User session, and/or into a 30 day cookie.

Then you can check the session on each succeeding form - if there is no ID then return to the first form; if there is one add it to a hidden input in the form.

The cookie is only useful if you think that users may take a longer time to complete the form steps. The user session is destroyed after 15 minutes of inactivity (you can lengthen the this if necessary). A cookie will only work if the user returns on the same computer though.

Here's an example of the code to do this in ChronoForms v3 - the first part is now built into the action for CFv4.

OnSubmit code for Form 1, the 'chronoforms_lijsten' table is just used to store a list of IDs.
$db =& JFactory::getDBO();
$lijst_id = JRequest::getString('lijst_id', '', 'post');
if ( !$lijst_id ) {
	do {
		$lijst_id = generateIdent('9999A');
		$query = "
			SELECT COUNT(*)
				FROM `#__chronoforms_lijsten`
				WHERE `lijst_id` = '{$lijst_id}' ;
		";
		$db->setQuery($query);
		$count = $db->loadResult();
	} while ($count != 0);
	$lijst_id = 'LSR'.$lijst_id;
}
$session = JFactory::getSession();
$session->set('lijst_id', $lijst_id);

// saved lijst_id to cookie for 30 days
setcookie('lijst_id', $lijst_id, time()+60*60*24*30);
JRequest::setVar('lijst_id', $lijst_id, 'post');

// save lijst_id to database
if ( !class_exists('Tablechronoforms_lijsten') ) {
	class Tablechronoforms_lijsten extends JTable {
		var $cf_id = null;
		var $uid = null;
		var $recordtime = null;
		var $ipaddress = null;
		var $cf_user_id = null;
		var $lijst_id = null;

		function __construct( &$database ) {
			parent::__construct( 'jos_chronoforms_lijsten', 'cf_id', $database );
		}
	}
}

and the code to check in the ON Load event of the later forms
$lijst_id = JRequest::getString('lijst_id', '', 'cookie');
$session = JFactory::getSession();
$lijst_id = $session->get('lijst_id', $lijst_id);
if ( !$lijst_id ) {
	$mainframe =& JFactory::getApplication();
	$mainframe->redirect('index.php?option=com_chronocontact&chronoformname=gedeelte_h1');
}

Bob
This topic is locked and no more replies can be posted.