PHP dynamic form elements

In a previous we explained how to use PHP to create a dynamic SELECT menu. The same way it’s possible to use PHP to create other form elements.

Create a radio group with PHP

Creating radio elements in some content management systems can result into writing a lot of code. Just in case your radio group with multiple options is getting a value from a database and/or a form. There need to be a check for every posted value for every option. This function will do all this work for you, just create two arrays, one for the values and labels and for the related html code. It’s up to the user if he uses 2 or more elements in one group. The function works with $_POST and $_GET data.

Select menu with optgroup elements

This example is about the optgroup element, with this “label” it is possible to create really user friendly select menu’s. Think about a menu with several product groups and related categories, with the optgroup element it’s possible to group the values like in a nested repeated list. The function creates a complete XHTML valid select menu with all option groups (product groups) and options (categories). Within the onchange event the form form will be submitted by default (leave the $form_name argument empty to disable this feature.

Populate select elements with PHP / MySQL

This small function is used to build a select menu with the values from a database table. Create the SQL statement outside the function with two values, one for the (option) value and one for the (option) label. The function is completely dynamic, you can change the name for the html elements (label and select) and you can use all tables you like.

Create form fields with PHP

This is another dynamic form element from my collection. I use this function in forms where a value of a database will be filled inside the form field. This is an easy way to get the right value inside the field if the form is submitted before and/or the process is stopped because some validation issue occurs. You can use the (optional) database value attribute as a default value, too.