Forums

Dropdown Filtering Based on Previous Dropdown Value

thejackswild 22 Jul, 2010
I am developing an entry form for a competition which at its simplest does the following:

1. User selects club from dropdown
2. User selects member id (CRN) from dropdown filtered by selected club

PHP is not my first language and while I know exactly how I would write this in VB, I am struggling with setting the club id as a variable to filter and requery the team member ids belonging to that club. I have been searching and reviewing topics to find a solution but have thus far been unsuccessful. I apologize in advance if the solution has already been posted somewhere here!

<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: 150px;">Club</label>
    <select class="cf_inputbox" id="select_1" size="1" title=""  name="Club">
    <option value="">Choose Option</option>

<?php
$db =& JFactory::getDBO();
$query = "
   SELECT Club
      FROM jos_chronoforms_frmFBClub ;
";

$db->setQuery($query);
$column= $db->loadResultArray();

?>


<?php
foreach ($column as $value)
  {
  echo '<option>'.$value.'</option>';
  }
?>
    </select>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: 150px;">CRN</label>
    <select class="cf_inputbox" id="select_2" size="1" title=""  name="CRN">
    <option value="">Choose Option</option>

<?php
$db =& JFactory::getDBO();
$query = "
   SELECT CRN, Club
      FROM jos_chronoforms_frmFBDogs ;
";

$db->setQuery($query);
$column= $db->loadResultArray();

?>


<?php
foreach ($column as $value)
  {
  echo '<option>'.$value.'</option>';
  }
?>
    </select>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Submit" name="button_3" type="submit" />
  </div>
  <div class="cfclear"> </div>
</div>


Thank you in advance!
Liz
GreyHead 26 Jul, 2010
Hi Liz,

There are three of ways of doing this .

The simplest is to make this a two-page form and re-load once the club is selected.

If the number of entries in the membership list not too long then you can load them all into the Form code using Option groups to separate them into clubs and then JavaScript to hide/display the groups.

Or, you can create a form using Ajax to look up the list for the selected club. A bit more complicated but works fine.

There are examples of all three in the forums here.

The main difference here from VB (I think) is that the PHP is executed before the form loads so anything that responds to user input has to be handled either by reload or by JavaScript.

Bob
ialex 03 Nov, 2010
"If the number of entries in the membership list not too long then you can load them all into the Form code using Option groups to separate them into clubs and then JavaScript to hide/display the groups."

how can i do that?
GreyHead 03 Nov, 2010
Hi iAleX,

Which part do you need? Where is the list you need to load?

The basic option group syntax is:
<optgroup label='group1'>
  <option . . .
  . . .</option>
</optgroup>
<optgroup label='group2'>
  <option . . .
  . . .</option>
</optgroup>

Bob
ialex 05 Nov, 2010
well, i need the form code (how to put groups into a dropdown).... -> i think you already answered that...

i also need the javascript to hide groups... i'm really newbie...

thanks bob!
This topic is locked and no more replies can be posted.