Back to Top

Populating custom forms with data from other components

Here's a small tutorial explaining how to populate a custom form (using ChronoForms) with information from the Joomla! database...you can use this to pull data from any of your installed Joomla! extensions (as long as you know how to find the right information).  It's pretty straightforward and works a treat...

What You'll Need

  • Direct access to your Joomla! database - PHPMyAdmin is best, although there are Joomla! extensions you can use.  This is just to establish the names of tables so you won't need to modify any data.
  • A copy of the marvellous ChronoForms extension installed to your Joomla! website.

For this tutorial I will use the brilliant Sigsiu SOBI2 as an example - I recently needed to pull the data from this extension and populate a form with that information (hence the reason for this tutorial).  In my case, I had a button in SOBI2 which linked to a ChronoForms form for an enquiry about the forwarding directory listing.

Step One: Create The Form

Joomla! 1.5 makes it easier to create the database queries

Ok, this assumes you have some knowledge of how ChronoForms works; I won't go into detail, but the form(s) need to be built in HTML (just a normal HTML form) then added to the Form Code section in ChronoForms.  The form can be anything you like, but generally involves an enquiry or contact element of sorts.

Once you've built your form, check that it functions correctly in Joomla!.  We'll be using the same form but instead of offering blank inputboxes we'll be populating the inputboxes with our data from our database.

Step Two: Fetch the Data

Joomla! 1.5 makes it easier to create the database queries by referencing the config settings to get the database info, so you don't need to add usernames and passwords (and so on) into the code.

The following code needs to be added at the top of your form code, and must be enclosed in PHP tags:

<?php

First we need to get the database prefix, normally "jos_":

$db =& JFactory::getDBO(); 

Then, we set up our database query:

$db->setQuery('SELECT title FROM #__sobi2_item WHERE itemid="1"');

In the example above, the bits in red relate to the parts of the database we need to get our information from.

sobi2_item - this is the database table where our specific information resides.  In PHPMyAdmin, you will see this in the list of database tables as "jos_sobi2_item" (assuming you're using the default DB prefix).

title - this is a database ROW within the jos_sobi2_item TABLE.  In this example, jos_sobi2_item->title contains the directory listing titles for my SOBI2 listings.

itemid - the itemid is another ROW within the jos_sobi2_item TABLE.  This is our reference so that we can pick specific, related items from the table.  I will explain later how to make this dynamic, but for the example above we're just using the data where the itemid is equal to "1".

So, in summary, this query says "tell me the name of the TITLE from the jos_sobi2_item database table, where the itemid is equal to 1".  Simple, right?

The next step is to load the result from the query into a variable for later use:

$databaseTitle = $db->loadResult(); 

...and there we go.  Our title data is stored into the variable called $databaseTitle.  Of course, you can call this variable anything you like.

You can now setup more queries and load the results into more variable for use in the form.  In SOBI2, the listing data/fields from the directory are stored in the table jos_sobi2_fields_data.  You need to browse the data in the tables you want to use (using PHPMyAdmin) carefully so you can work out the names of the rows you need to reference in your query.  For example, my next query might want a contact email address from the listing.  My email address field is stored in a field with a fieldid value of "15".  Again, this uses the same itemid so the values can all tie up to the same item in the database.  So, my next query might look like this:

$db->setQuery('SELECT data_txt FROM #__sobi2_fields_data WHERE
fieldid="15" AND itemid="1"');
$databaseContact = $db->loadResult();

Note there is no reason to add the database prefix query a second time, this only needs to be done once.

data_txt is the generic name for the user data contained within this table.

You can continue to add queries in this way, until you have all the data you want, loaded ready to use in a set of variables.

Once finished, don't forget to close your PHP:

?>

Step Three: Using the Variables

So, we have our variables all ready to go - all we need to do now is display them where relevant into our form.  These can be used in pretty much any form element, and (in our case) in hidden fields so we can target the form to specific users.

First, we want to display the title of the listing that we're enquiring about:

<input type="text" name="title" id="title" class="inputbox"
value="<?php echo $databaseTitle; ?>" />
ChronoForms allows you to send forms to dynamic email addresses

This will display a standard form inputbox, but will pre-populate the box with the value of our $databaseTitle variable.

Next, I want to have my form emailed to the contact person who is referenced in the original listing - for example, I might have lots of listings with different contact email addresses.  ChronoForms allows you to send forms to dynamic email addresses from a form field.  In this case, I don't want this information displayed on the form layout, so will add it to a hidden field:

<input type="hidden" name="sendto" id="sendto"
value="<?php echo $databaseContact; ?>">

This will make a hidden (non-displayed) form field called "sendto" and will populate it with our contact's email address.  You can use the "sendto" value in the ChronoForms email setup, for the "Dynamic To" field.

Making the ItemID Dynamic

This is all well and good, but how do you make the database query more dynamic, instead of referencing a fixed number?

In this case, at least, we have easy access to SOBI2's template layout, so can customise our forwarding link in order to add in a reference to our SOBI2 itemid value.  Our outgoing link (from SOBI2) might look like this:

<a href="index.php?option=com_chronocontact&amp;chronoformname=name_of_form
&amp;sid=<?php echo $mySobi->id; ?>">Enquire</a>

This will create the following link within the SOBI2 details page:

index.php?option=com_chronocontact&chronoformname=name_of_form&sid=xx

sid=xx in this case will be the itemid of the current SOBI2 listing, and "sid" can be named however you like.  We will use this to reference the request data from the URL inside ChronoForms:

$sid = $_REQUEST['sid'];

This will simply store the value of "sid" into a variable called $sid.

We can now make our query dynamic based on this value like this:

$db->setQuery('SELECT title FROM #__sobi2_item WHERE itemid="'.$sid.'"');

...and so on.

Hopefully that will give you some starters for making some pretty neat custom forms in Joomla!.  Don't forget, you can use the same code to pull all sorts of data from lots of different components.  The key is knowing the names of the tables and rows in the database.  Don't forget to follow us on Twitter for more useful Joomla! hints!