formfields.com Forum Index formfields.com
FORMfields Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How Do I Create Dynamic Drop Down Menus?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    formfields.com Forum Index -> FF 2.X - Frequently Asked Questions (FAQs)
View previous topic :: View next topic  
Author Message
formfields



Joined: 01 Aug 2005
Posts: 465

PostPosted: Wed Nov 30, 2005 8:01 pm    Post subject: How Do I Create Dynamic Drop Down Menus? Reply with quote

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.
Code:

<? /* BEGIN - SECTION 1 */ ?>
<?php
   
   require($_SERVER["DOCUMENT_ROOT"] . "/FORMfields/FORMfields.php");

   $formBean = new FormBean();
   $formBean->addField(new SubmitField("_submit", "Submit"));
   $formBean->addField(new SubmitField("clear", "Clear"));
   $formBean->addField(new SubmitField("cancel", "Cancel"));
   
   $genre = array("Rock", "Hip Hop");
   $formBean->addField(new DropDownField("genre", "Music Genre", REQUIRED, $genre, $genre, "[Select One]"));
   $formBean->formFields["genre"]->setExtraHtml("onchange=\"document.form1.submit();\"");

   if ($_REQUEST["genre"] === "Rock") {
      $bands = array("Dave Matthews Band", "Coldplay");
   } else if ($_REQUEST["genre"] === "Hip Hop") {
      $bands = array("Tribe Called Quest", "Roots");
   }
   
   if ($_REQUEST["genre"]) { // if a value is selected
      $formBean->addField(new DropDownField("band", "Favorite Band", REQUIRED, $bands, $bands, "[Select One]"));
   }

   $formBean->getParameters();
   
?>
<? /* END - SECTION 1 */ ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form1</title>
<? /* BEGIN - SECTION 2 */ ?>
<link rel="stylesheet" type="text/css" href="/FORMfields/FORMfields.css" />
<script type="text/javascript" src="/FORMfields/FORMfields.js"></script>
<link rel="stylesheet" type="text/css" href="/FORMfields/tableHelpers.css" />
<? /* END - SECTION 2 */ ?>
</head>

<body onload="setFocus();">
<? /* BEGIN - SECTION 3 */ ?>
   <form name="form1" action="" method="get">
      <div class="FORMfields">
         <?= $formBean->getTableTag() ?>
      </div>
   </form>
<? /* END - SECTION 3 */ ?>
</body>


</html>
Back to top
View user's profile Send private message
formfields



Joined: 01 Aug 2005
Posts: 465

PostPosted: Mon Apr 17, 2006 5:04 pm    Post subject: Reply with quote

Here is an example of how to load a drop down box from rows in a database table:

Let's say you have a table named friends with the following structure and data:
fields: id name age
row1: 1 John 30
row2: 2 Peter 34
row3: 3 Bob 29

Then the code to display a drop down box of friends would be like:
Code:

<? /* BEGIN - SECTION 1 */ ?>
<?php
   
   require($_SERVER["DOCUMENT_ROOT"] . "/FORMfields/FORMfields.php");

   $formBean = new FormBean();
   $formBean->addField(new SubmitField("_submit", "Submit"));
   $formBean->addField(new SubmitField("clear", "Clear"));
   $formBean->addField(new SubmitField("cancel", "Cancel"));
   
   $friends = getList("SELECT name FROM friends");
   $formBean->addField(new DropDownField("friend", "Friend", REQUIRED, $friends, $friends, "[Select One]"));

   $formBean->getParameters();
   
?>
<? /* END - SECTION 1 */ ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form1</title>
<? /* BEGIN - SECTION 2 */ ?>
<link rel="stylesheet" type="text/css" href="/FORMfields/FORMfields.css" />
<script type="text/javascript" src="/FORMfields/FORMfields.js"></script>
<link rel="stylesheet" type="text/css" href="/FORMfields/tableHelpers.css" />
<? /* END - SECTION 2 */ ?>
</head>

<body onload="setFocus();">
<? /* BEGIN - SECTION 3 */ ?>
   <form name="form1" action="" method="get">
      <div class="FORMfields">
         <?= $formBean->getTableTag() ?>
      </div>
   </form>
<? /* END - SECTION 3 */ ?>
</body>

</html>
Back to top
View user's profile Send private message
formfields



Joined: 01 Aug 2005
Posts: 465

PostPosted: Mon May 08, 2006 4:51 pm    Post subject: Reply with quote

If you are using FORMgen's Confirmation Screen option with the 1st dynamic drop example above, then you have to take in consideration values passed by hidden fields. FORMfields uses hidden values to pass around the values on the Confirmation Screen. Therefore, the example would become:
Code:

...
   $genre = $_REQUEST["genre"];
   if (!$genre)
     $genre = $_REQUEST["genre__hidden"];

   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
      $formBean->addField(new DropDownField("band", "Favorite Band", REQUIRED, $bands, $bands, "[Select One]"));
   }
...
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    formfields.com Forum Index -> FF 2.X - Frequently Asked Questions (FAQs) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group