HomeDocumentationTutorialsIncluding a checkbox using data from a database field

Including a checkbox using data from a database field

Overview

In this tutorial, we'll cover how to add a checkbox to your forms. This method allows you to integrate checkbox inputs seamlessly into your application forms. Additionally, we will demonstrate how to configure the checkbox to read its option data from a field in a database using the childDatabase method. This method adds a checkbox  input field to your form, taking three parameters:

 

  • field_name: The name attribute for the checkbox  .
  • field_label: The label that will be displayed alongside the checkbox  .
  • $required: An optional parameter (default is 0) that specifies whether the checkbox input is required (1) or not (0).

 

To configure the checkbox to read its option data from a field in a database, you can chain the childDatabase method after the checkbox method. This example shows how to add a checkbox that retrieves its options from a database table.

 

PHP
 // Adding a checkbox with options from the database
$objConfig->checkbox('field_name', 'field_label', $required = 0)->childDatabase('table_name', 'id_field', 'value_field');    

 

In this example:

 

  • table_name: The name of the database table containing the options.
  • id_field: The field in the database table that serves as the unique identifier for each option.
  • value_field: The field in the database table that contains the display name of each option.

 

Configuration in Your Application

For the configuration to work properly, it's important to include the 'field_name'  amongst the `addFields` and `editFields`. This ensures that the fields are correctly initialized and integrated into your form. This example shows how to configure the add and edit fields.

 

PHP
// Adding your checkbox field to the add and edit forms
$objConfig->addFields(array('field_name'));
$objConfig->editFields(array('field_name'));

 

Conclusion

By following these steps, you can easily integrate checkbox fields to read its option data from a field in a database using $objConfig->checkbox('field_name', 'field_label', $required = 0)->childDatabase('table_name', 'id_field', 'value_field');.