Display all the options if nothing is selected

Posted by Levani on Stack Overflow See other posts from Stack Overflow or by Levani
Published on 2010-05-29T12:45:01Z Indexed on 2010/05/29 13:12 UTC
Read the original article Hit count: 289

Filed under:
|
|

I have four level connected dropdown boxes. I can choose something from second dropdown only after I select something from the first one. The third dropdown is also depended on second one and so on.

I need to make selection available in second, third and fourth dropdown, even if there isn't selected anything in previous dropdown, but if the selection is made behave as it behaves now.

Am I clear?

Here is the code:

<table width="100%" border="0" cellspacing="1" cellpadding="4">
    <!-- *********************** Countries *********************** -->
    <tr>
      <td style="width:150px"><?php if($eaconf->ea_loc_srchtype==0) echo "<b>".JText::_('EA_SRCH_STEP')."</b> 1: ";?><?php echo JText::_('EA_OBJ_COUNTRY'); ?></td>
      <td >
        <?php
        $countrylist[] = JHTML::_('select.option', 'no', JText::_('EA_NOT_SELECTED'));
        $x=0;
        foreach($countries as $c){
            $countrylist[] = JHTML::_('select.option', $c ,JText::_($countrynames[$x]));
            $x++;
        } 
        if($eaconf->ea_loc_srchtype==0){
            $countrys['src_country'] = JHTML::_('select.genericlist', $countrylist,'src_country',' class="inputbox" style="width:140px"  onChange="setStates(this.selectedIndex-1)"', 'value', 'text','0' );
        }else{
            $countrys['src_country'] = JHTML::_('select.genericlist', $countrylist,'src_country',' class="inputbox" style="width:140px" ', 'value', 'text','0' );
        } ?>
        <?php echo $countrys['src_country'];?>
      </td>
    </tr>
    <!-- *********************** States *********************** -->
    <tr>
      <td ><?php if($eaconf->ea_loc_srchtype==0) echo "<b>".JText::_('EA_SRCH_STEP')."</b> 2: ";?><?php echo JText::_('EA_OBJ_STATE'); ?></td>
      <td >
        <?php
        $statelist[] = JHTML::_('select.option', 'no', JText::_('EA_NOT_SELECTED'));
            foreach($stateslist as $s){
                $statelist[] = JHTML::_('select.option', $s ,JText::_($s));
            } 
        if($eaconf->ea_loc_srchtype==0){
            $thestates['src_state'] = JHTML::_('select.genericlist', $statelist,'src_state',' class="inputbox" style="width:140px"  onChange="setDistricts(this.selectedIndex-1)"', 'value', 'text','0' );
        }else{
            $thestates['src_state'] = JHTML::_('select.genericlist', $statelist,'src_state',' class="inputbox" style="width:140px" ', 'value', 'text','0' );
        } ?>

        <?php echo $thestates['src_state'];?>
      </td>
    </tr>
    <!-- *********************** Districts ******************** -->
    <tr>
      <td  >
            <?php if($eaconf->ea_loc_srchtype==0) echo "<b>".JText::_('EA_SRCH_STEP')."</b> 3: ";?><?php echo JText::_('EA_OBJ_DISTRICT'); ?>
      </td>
      <td >
        <?php
            $districtlist[] = JHTML::_('select.option', 'no',JText::_('EA_NOT_SELECTED'));

            foreach($districtslist as $dist){
                    $districtlist[] = JHTML::_('select.option', $dist ,JText::_($dist));
            } 
            if($eaconf->ea_loc_srchtype==0){
                $thedistrict['src_district'] = JHTML::_('select.genericlist', $districtlist,'src_district',' class="inputbox" style="width:140px"  onChange="setTowns(this.selectedIndex-1)"', 'value', 'text','0' );
            }else{
                $thedistrict['src_district'] = JHTML::_('select.genericlist', $districtlist,'src_district',' class="inputbox" style="width:140px" ', 'value', 'text','0' );
            } ?>

        <?php echo $thedistrict['src_district'];?>
      </td>
    </tr>

    <!-- *********************** Towns ************************* -->    
    <tr>
      <td ><?php if($eaconf->ea_loc_srchtype==0) echo "<b>".JText::_('EA_SRCH_STEP')."</b> 4: ";?><?php echo JText::_('EA_OBJ_TOWN'); ?></td>
      <td >
        <?php
        $townlist[] = JHTML::_('select.option', 'no',JText::_('EA_NOT_SELECTED'));
            foreach($townslist as $town){
                $townlist[] = JHTML::_('select.option', $town ,JText::_($town));
            } 
        if($eaconf->ea_loc_srchtype==0){
            $towns['src_town'] = JHTML::_('select.genericlist', $townlist,'src_town',' class="inputbox" style="width:140px" ', 'value', 'text','0' );
        }else{
            $towns['src_town'] = JHTML::_('select.genericlist', $townlist,'src_town',' class="inputbox" style="width:140px" ', 'value', 'text','0' );
        } ?>

        <?php echo $towns['src_town'];?>
      </td>
    </tr>
    </table>

And the javascript:

function setStates(scountry) {

        sel_state = document.easearch.elements["src_state"];
        sel_town = document.easearch.elements["src_town"];
        sel_district = document.easearch.elements["src_district"];
        empty("src_state");

        if(scountry=='no') {
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_state.options.length;
            sel_state.options[new_opt] = new_entry;
            sel_state.options[new_opt].value = "no";
            sel_state.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";



            empty("src_district");
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_district.options.length;
            sel_district.options[new_opt] = new_entry;
            sel_district.options[new_opt].value = "no";
            sel_district.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";

             empty("src_town");
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_town.options.length;
            sel_town.options[new_opt] = new_entry;
            sel_town.options[new_opt].value = "no";
            sel_town.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";
        }
        else{
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_state.options.length;
            sel_state.options[new_opt] = new_entry;
            sel_state.options[new_opt].value = "no";
            sel_state.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";
            for (x=0;x<States[scountry].length;x++ ){
                new_entry = new Option(States[scountry][x]);
                new_opt = sel_state.options.length;
                sel_state.options[new_opt] = new_entry;
                sel_state.options[new_opt].value = x;
                sel_state.options[new_opt].text = States[scountry][x];
            }

            empty("src_district");
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_district.options.length;
            sel_district.options[new_opt] = new_entry;
            sel_district.options[new_opt].value = "no";
            sel_district.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";

            empty("src_town");
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_town.options.length;
            sel_town.options[new_opt] = new_entry;
            sel_town.options[new_opt].value = "no";
            sel_town.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";


        }
    }



    function setDistricts(sstate) {

        scountry = document.easearch.src_country.selectedIndex-1;
       //   sstate = document.easearch.src_state.value;
        sel_district = document.easearch.elements["src_district"];
        empty("src_district");
        if(sstate=='no') {
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_district.options.length;
            sel_district.options[new_opt] = new_entry;
            sel_district.options[new_opt].value = "no";
            sel_district.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";

            empty("src_town");
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_town.options.length;
            sel_town.options[new_opt] = new_entry;
            sel_town.options[new_opt].value = "no";
            sel_town.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";
        }
        else{
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_district.options.length;
            sel_district.options[new_opt] = new_entry;
            sel_district.options[new_opt].value = "no";
            sel_district.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";

            for (x=0;x<Districts[scountry][sstate].length;x++ ){
                new_entry = new Option(Districts[scountry][sstate][x]);
                new_opt = sel_district.options.length;
                sel_district.options[new_opt] = new_entry;
                sel_district.options[new_opt].value = x;
                sel_district.options[new_opt].text = Districts[scountry][sstate][x];
            }

            empty("src_town");
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_town.options.length;
            sel_town.options[new_opt] = new_entry;
            sel_town.options[new_opt].value = "no";
            sel_town.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";
        }
    }

     function setTowns(sdistrict) {

        scountry = document.easearch.src_country.selectedIndex-1;
        sstate = document.easearch.src_state.value;
        sel_town = document.easearch.elements["src_town"];
        sel_district = document.easearch.elements["src_district"];

        empty("src_town");
        if(sdistrict=='no') {

            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_town.options.length;
            sel_town.options[new_opt] = new_entry;
            sel_town.options[new_opt].value = "no";
            sel_town.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";

            empty("src_district");
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_district.options.length;
            sel_district.options[new_opt] = new_entry;
            sel_district.options[new_opt].value = "no";
            sel_district.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";
        }
        else{
            new_entry = new Option("<?php echo JText::_('EA_NOT_SELECTED');?>");
            new_opt = sel_town.options.length;
            sel_town.options[new_opt] = new_entry;
            sel_town.options[new_opt].value = "no";
            sel_town.options[new_opt].text = "<?php echo JText::_('EA_NOT_SELECTED');?>";
            for (x=0;x<Towns[scountry][sstate][sdistrict].length;x++ ){
                new_entry = new Option(Towns[scountry][sstate][sdistrict][x]);
                new_opt = sel_town.options.length;
                sel_town.options[new_opt] = new_entry;
                sel_town.options[new_opt].value = x;
                sel_town.options[new_opt].text = Towns[scountry][sstate][sdistrict][x];
            }
        }
    }

    function empty(field) {
        document.easearch.elements[field].options.length = 0;
    }

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript