FORMfields, the premiere web framework
Login
Support Home | Knowledge Base | Documentation | Forums | Contact Us
Info
FORMfields & FORMgen :: Field Customizations

Dynamic DropDownFields

(2502 views. Last Updated: 2007-08-13 7:36 PM)

Here is an example of a FORMfields form where there are two drop down boxes:
1. A music genre box
2. A dynamic band box, which is populated with bands in the chosen music genre.

To create this example, use FORMgen to create a form with a DropDownField with the options "Rock" and "Hip Hop"
Then edit your form and replace:
// ---------- FORM STRUCTURE - START ----------

$ffForm = new FfForm();
$ffForm->addField(new DropDownField("genre", "Music Genre", FORM_FIELD_REQUIRED, array("Rock", "Hip Hop"), null, "[Select One]"));
with:
// ---------- FORM STRUCTURE - START ----------

$ffForm = new FfForm();

$genres = array("Rock", "Hip Hop");
$ffForm->addField(new DropDownField("genre", "Music Genre", FORM_FIELD_REQUIRED, $genres, null, "[Select One]"));
$ffForm->formFields["genre"]->setExtraHtml("onchange=\"document.form1.submit();\"");

$genre = FormField::getOrNull("genre", $_REQUEST);
if (!$genre)
    $genre = FormField::getOrNull("genre__hidden", $_REQUEST); // FORMfields uses hidden values to pass arund the values on the Confirmation Screen

if ($genre === "Rock") {
    $bands = array("Dave Matthews Band", "Coldplay");
} else if ($genre === "Hip Hop") {
    $bands = array("Tribe Called Quest", "Roots");
}

if ($genre) { // if a value is selected
    $ffForm->addField(new DropDownField("band", "Favorite Band", FORM_FIELD_REQUIRED, $bands, null, "[Select One]"));
}
Info
Support | Earn Money
Copyright © 2005-2019 Brain Book Software LLC.
Built with FORMfields, the premiere web framework.