Home
In Development
Custom DB Tables in Breezing Forms
Custom DB Tables in Breezing Forms
I have a site where a user can fill out a form (created with the Breezing Forms plugin). Among other things, it stores the users's Joomla! user id so the form data can be associated to that user. In other words, this site needs more user information than is typically collected for Joomla! users.

I'm going to modify the form to store the data collected in an alternate table than #__facileforms_subrecords. I want this data to be stored in client_user_data instead. Also, I want to modify the form so that if the current user has filled it out before, it pre-fills all the fields with the old data, and the submit button updates that record, rather than creating a new record in the table client_user_data.
Here's a highlight of the fields that store Joomla! data from the registered user.
And this is how I get that Joomla! data into those fields:
The first thing I want to do is change where this form stores its data from the default BF table, to another one. Primarily, I am doing this to make sure that a user can only fill this form out one time.
So, I go to the form, click the edit form button, and select the submit pieces tab. I'm going to change the Begin Submit from none to custom, and put some php in there to check the table for a form for this user's id, and update it, if it's there, or submit this form if no record exists.

Here's what's inside that box (less a few fields for the sake of brevity)
// we need to get the data out of the soon-to-be post vars
// extract form data
foreach ($this->submitdata as $data)
switch ($data[_FF_DATA_NAME]) {
case 'address':
$address = $data[_FF_DATA_VALUE];
break;
case 'city':
$city = $data[_FF_DATA_VALUE];
break;
case 'state':
$state = $data[_FF_DATA_VALUE];
break;
default:
break;
} // switch
//get Joomla! objects we need
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$result = null;
$query = "SELECT joomla_id from client_user_data where joomla_id = " . $user->id;
$db->setQuery($query);
$result = $db->loadObject();
if ($db->getErrorNum()) {
JError::raiseWarning( 500, $db->stderr() );
}
if ($result) { // we have a Joomla! user record already, let's update it
$query = "update client_user_data set address = '" . $address . "', ";
$query .= "city = '" . $city . "', ";
$query .= "state = '" . $state . "' ";
$query .= "where joomla_id = " . $user->id;
} else { // This is a new submission, insert it
$query = "insert into client_user_data values (" . $user->id . ", '";
$query .= $address . "', '" . $city . "', '" . $state . "')";
}
echo $query;
$db->setQuery($query);
if (!$db->query()) {
JError::raiseError( 500, $db->stderr());
}
Now when a user submits this form, the data doesn't go into the (slightly confusing to human readers) jos_facileforms_subrecords table. It all goes into the client_user_data table. Some day, we'll be able to build a module that allows this site's owner to view the completed forms.
It stores and updates now, but if a user has already filled out the form, we don't want a blank form if he or she navigates to this page ever again. So, let's add something to query the database and pre-fill the form with current data if this user already has a copy of this form on file.
Inside the Edit Form screen, on the Before Form tab, open the custom field:
Here's the complete bit in the box:
$db =& JFactory::getDBO();
$user =& JFactory::getUser();
$result = null;
$query = "SELECT * from client_user_data where joomla_id = " . $user->id;
$db->setQuery($query);
$result = $db->loadObject();
if ($db->getErrorNum()) {
JError::raiseWarning( 500, $db->stderr() );
}
$_POST['name'] = $user->name;
if ($result) { // found one
$_POST['address'] = $result->address;
$_POST['city'] = $result->city;
$_POST['state'] = $result->state;
}
The only thing remaining is to alter the fields to ensure that they think to check current $_POST values.
Edit the element itself and use something like this for a selector:
if (isset($_POST['collections'])) {
$which = $_POST['collections'];
} else {
$which = "notyet";
}
if (strcmp($which, "yes")==0) {
$options = "1;Yes;yes\n";
} else {
$options = "0;Yes;yes\n";
}
if (strcmp($which, "no")==0) {
$options .= "1;No;no\n";
} else {
$options .= "0;No;no\n";
}
return $options;
or for a text field:
if (isset($_POST['total_debt_load'])) {
return $_POST['total_debt_load'];
} else {
return;
}
Now the form will display empty to anyone who has never filled it out, and it will pre-fill for anyone who has. Any changes the user makes in the form are updated to the database on submit.
NOTE: Inside the form element Options box, you need to wrap the php code in <??> but you do not inside the Edit Form boxes.
Trackback(0)
TrackBack URI for this entryComments (5)
Subscribe to this comment's feedCan you help me
DO I have to make those tables in phpmyadmin?
Can you help me
Do I have to create it in phpmyadmin as tables?
...
SQL-Code
Can someone please post the MySQL Code or the Entity-relationship model?
...
Create the table using phpMyAdmin or the following (similar) SQL:
create table jos_client_user_data
CREATE TABLE IF NOT EXISTS `jos_client_user_data` (
`user_id` int(11) NOT NULL default '0',
`whatever_field_you_like` varchar(32) default NULL,
PRIMARY KEY (`user_id`)
)
Write comment

Last Updated (Monday, 14 July 2008 20:14)





