Buy Now
Sign in

FAQ search

How to load one row from a DB table into your form

Published: Monday, 30 November -0001 00:00
In the last tutorial we created a form which saves some form data into a database table, but what if we want to load record data from that table into our form? Let's see how this can be done:

This FAQ is based on the downloadable PDF tutorial written by Max here.

  1. We are going to use the same form which we used in our last tutorial for the DB Save action. Open the form for editing in the Wizard using the “Wizard edit” link in the Forms Manager.
  2. Drag a 'DB Record Loader' action to the 'On Load' Event of the form, make sure its placed before the 'Show HTML' action. 
  3. Next let's configure the new DB Record Loader action.
  4. First, we need to decide how are we going to load the record from the database table. In this tutorial we are going to pass the value of the cf_uid field into the form URL to load the corresponding record from the table, since the cf_uid is unique for each record then this should be OK. So write cf_uid in the DB Field option box, then let's select our table (either the same table as the previous tutorial or whichever table the data is stored in).
  5. We assume in this example that we are going to pass the cf_uid field value to the form URL. We need to identify which parameter name we will use to pass the field value. We’ll call it token. Here is how it will look in the URL when supplied:
  6. index.php?option=com_chronoforms&chronoform=user_details&token=ff2119500f1a4ec2318c210bf681b0fe
  7. To get the data into our form, set up the DB Record Loader | Basic fields as shown in the figure below. Note that we are using the cf_uid field values to load a record into the form, and we will supply that value in the URL. column names exactly or the data sill not be loaded correctly.
  8. In the image:
    1. DB Field is where we identify which database table column to use for the record lookup. In our case, we will use the cf_uid column.
    2. Table is where we identify which table in our database we will be looking into for the record matching the cf_uid value.
    3. Request Param is the name we will use in the URL to specify the value to use in the database record lookup.
  9. Although there are other settings to configure, they are not needed now. You can use the WHERE box to write a custom query that is more flexible than using a single DB Field and value pair. The Model ID settings can be helpful when loading data from multiple tables into one form with different field names, and so on. For now, just set your form the way you see in the figure above.
  10. Click “Apply” and then close the popup and click “Save”. Now, if you select the Frontend View option, your URL will look something like this. Notice that it ends with the form name (user_details in this example), and “token” does not appear in it at all.
  13. Let’s test our form as usual, using the default URL (without the token param). It should be empty as before.
  14. To get the data record into the form, append the URL with the token value using the text &token= followed by the cf_uid value of the record.
  16. Now we see the data !!
  17. What if I clicked the submit button now? What would happen?
  18. A new database record would be created, but of course we usuaully want to update the loaded record. To do this go back to the Wizard Edit and drag a “Hidden field” to your form Preview box and edit its name to cf_id which is the 'Primary Key' of the connected table. Then the loaded record will be updated when the form is submitted instead of creating a new one.

Note that you can use the loaded data in many ways in your form. Please see this FAQ for more information.

In this example the record identifier was passed in a URL; you could also pass it from another form; from data saved in the User session; in a Cookie from a previous visit; or from the current User ID if you wish to.

Thanks to user GardinerJ for improved text and images for a large part of this tutorial