Skip to main content
Inspiring
July 16, 2018
Answered

Multiple search fields using drop down menus

  • July 16, 2018
  • 5 replies
  • 1315 views

Hi everyone,

I am trying to create a simple order form that needs to include multiple search fields.

For example, let's say your selling hats. The first drop down menu allows you to select your hat of choice. The second drop down menu, based on the hat you selected, will show those colors or sizes available.

The second drop down menu would/could be different for each hat style. The options that appear in the second drop down menu are dependent on which hat you choose.

This is standard procedure for most order forms. Any help on how to make this work would be appreciated. Even a pre-existing order form template would suffice.

Thanks.

Mark

    This topic has been closed for replies.
    Correct answer osgood_

    In its simplest form (without a database see code below). If you have dozens of hats with dozens of colors then really the only way is to store the information is in a database, for easy management.

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <title>Hat Choice</title>

    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="

    crossorigin="anonymous"></script>

    <script>

    $(document).ready(function(){

    $('.hat_colors').hide();

    $('.hat_option').change(function(){

    let hat_option = $('.hat_option').val();

    if(hat_option === "Small") {

    $('.hat_colors').show().html(

    '<option value="" selected>Select Color</option>' +

    '<option value="Pink">Pink</option>' +

    '<option value="Red">Red</option>' +

    '<option value="Green">Green</option>'

    )

    }

    if(hat_option === "Medium") {

    $('.hat_colors').show().html(

    '<option value="" selected>Select Color</option>' +

    '<option value="Orange">Orange</option>' +

    '<option value="Mauve">Mauve</option>' +

    '<option value="Black">Black</option>'

    )

    }

    if(hat_option === "Large") {

    $('.hat_colors').show().html(

    '<option value="" selected>Select Color</option>' +

    '<option value="Violet">Violet</option>' +

    '<option value="Grey">Grey</option>' +

    '<option value="Brown">Brown</option>'

    )

    }

    });

    });

    </script>

    </head>

    <body>

    <select name="hat_option" class="hat_option">

    <option value="" selected>Select Option</option>

    <option value="Small">Small</option>

    <option value="Medium">Medium</option>

    <option value="Large">Large</option>

    </select>

    <select name="hat_colors" class="hat_colors"></select>

    </body>

    </html>

    You could also use the jquery load function to keep the code a bit more managable if you are trying to do this using a non-database workflow. You would create seperate html files with the color options related to each hat and load the file based on which hat option was chosen

    <script>

    $(document).ready(function(){

    $('.hat_colors').hide();

    $('.hat_option').change(function(){

    let hat_option = $('.hat_option').val();

    if(hat_option === "Small") {

    $('.hat_colors').show().load('small_color_options.html')

    }

    if(hat_option === "Medium") {

    $('.hat_colors').show().load('medium_color_options.html')

    }

    if(hat_option === "Large") {

    $('.hat_colors').show().load('large_color_options.html')

    }

    });

    });

    </script>

    So for example in 'small_color_options.html' you would have the below code:

    <option value="" selected>Select Option</option>

    <option value="Pink">Pink</option>

    <option value="Red">Red</option>

    <option value="Green">Green</option>

    5 replies

    BenPleysier
    Community Expert
    Community Expert
    July 16, 2018

    I tend to agree with @osgood_ in this. I feel that using dependant dropdown lists is confusing for the casual user, although I do tend to use it for the Admin part of a website. But this is because the Admin is used to the system.

    As an example, see https://cobbcoleatherhats.com/product-lines.php

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    Legend
    July 16, 2018

    https://forums.adobe.com/people/Fun+Seeker  wrote

    For example, let's say your selling hats. The first drop down menu allows you to select your hat of choice. The second drop down menu, based on the hat you selected, will show those colors or sizes available.

    Not really.

    The first drop down or list of links would normally take you to another page listing the range of hats available in that category:

    Dress hats >

    Party Hats >

    Winter Hats >

    Dress Hats Available

    Dress Hat 1

    Image - click for details

    Dress Hat 2

    Image - click for details

    Dress Hat 3

    Image - click for details

    Dress Hat 1 Details

    Description: Blah Blah

    Colors Avaliable:

    Red

    Green

    Blue

    Sizes Available:

    Small

    Medium

    Large

    Price

    $14.25

    Add to Cart

    WolfShade
    Legend
    July 16, 2018

    Here is a vanilla JavaScript example that can be modified for use with a database:

    <select id="hats" name="hats">
        <option value="">SELECT</option>
        <option value="tophat">Top Hat</option>
        <option value="woolcap">Wool Cap</option>
        <option value="visor">Visor</option>
    </select>
    <select id="color" name="color"></select>
    <script>
        var topHatColorsVal = ['red','blue','white','green'],
            woolCapColorsVal = ['brown','tan','darkblue'],
            visorColorsVal = ['green','darkblue','red','yellow'];

        var topHatColorsTxt = ['Red','Blue','White','Green'],
            woolCapColorsTxt = ['Brown','Tan','Dark Blue'],
            visorColorsTxt = ['Green','Dark Blue','Red','Yellow'];

        var hats = document.getElementById('hats'),
            color = document.getElementById('color');
        hats.addEventListener('change',changeColors);

        function changeColors(){
            var colors = color.options.length;
            if(colors >= 0){
                for(x=colors;x>0;x--){
                    color.remove(x-1);
                    }
                }
            color.add(new Option('Choose',''));
            switch(hats.value){
                case 'tophat':
                    for(i=0;i<topHatColorsVal.length;i++){
                        color.add(new Option(topHatColorsTxt,topHatColorsVal));
                        }
                break;
                case 'woolcap':
                    for(i=0;i<woolCapColorsVal.length;i++){
                        color.add(new Option(woolCapColorsTxt,woolCapColorsVal));
                        }
                break;
                case 'visor':
                    for(i=0;i<visorColorsVal.length;i++){
                        color.add(new Option(visorColorsTxt,visorColorsVal));
                        }
                break;
                }
            }
    </script>

    HTH,

    ^ _ ^

    Inspiring
    July 17, 2018

    Hi Wolfshade, that's for offering the script but no database will be used. That, I'm afraid, is still way over my head.

    Thanks.

    Nancy OShea
    Community Expert
    Community Expert
    July 16, 2018

    Your shopping cart system should be able to handle product variables.  Which cart are you using?

    Nancy O'Shea— Product User & Community Expert
    Inspiring
    July 17, 2018

    Hi Nancy, thanks for responding. Actually, there is no shopping cart, only a form. I used the hat example for illustrative purposes only. I was looking for a way to have drop down menus work together, and this script is perfect.

    For example, let's say you have a school and offer classes on a variety of different subjects, each with multiple start dates. In the first menu you select the subject, and then use the second menu to select from the different start dates. This sort of thing.

    I'm sure it could be used on an online order form as well, but as you mentioned, most shopping cart software already provides for this.

    Thanks.

    osgood_Correct answer
    Legend
    July 16, 2018

    In its simplest form (without a database see code below). If you have dozens of hats with dozens of colors then really the only way is to store the information is in a database, for easy management.

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <title>Hat Choice</title>

    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="

    crossorigin="anonymous"></script>

    <script>

    $(document).ready(function(){

    $('.hat_colors').hide();

    $('.hat_option').change(function(){

    let hat_option = $('.hat_option').val();

    if(hat_option === "Small") {

    $('.hat_colors').show().html(

    '<option value="" selected>Select Color</option>' +

    '<option value="Pink">Pink</option>' +

    '<option value="Red">Red</option>' +

    '<option value="Green">Green</option>'

    )

    }

    if(hat_option === "Medium") {

    $('.hat_colors').show().html(

    '<option value="" selected>Select Color</option>' +

    '<option value="Orange">Orange</option>' +

    '<option value="Mauve">Mauve</option>' +

    '<option value="Black">Black</option>'

    )

    }

    if(hat_option === "Large") {

    $('.hat_colors').show().html(

    '<option value="" selected>Select Color</option>' +

    '<option value="Violet">Violet</option>' +

    '<option value="Grey">Grey</option>' +

    '<option value="Brown">Brown</option>'

    )

    }

    });

    });

    </script>

    </head>

    <body>

    <select name="hat_option" class="hat_option">

    <option value="" selected>Select Option</option>

    <option value="Small">Small</option>

    <option value="Medium">Medium</option>

    <option value="Large">Large</option>

    </select>

    <select name="hat_colors" class="hat_colors"></select>

    </body>

    </html>

    You could also use the jquery load function to keep the code a bit more managable if you are trying to do this using a non-database workflow. You would create seperate html files with the color options related to each hat and load the file based on which hat option was chosen

    <script>

    $(document).ready(function(){

    $('.hat_colors').hide();

    $('.hat_option').change(function(){

    let hat_option = $('.hat_option').val();

    if(hat_option === "Small") {

    $('.hat_colors').show().load('small_color_options.html')

    }

    if(hat_option === "Medium") {

    $('.hat_colors').show().load('medium_color_options.html')

    }

    if(hat_option === "Large") {

    $('.hat_colors').show().load('large_color_options.html')

    }

    });

    });

    </script>

    So for example in 'small_color_options.html' you would have the below code:

    <option value="" selected>Select Option</option>

    <option value="Pink">Pink</option>

    <option value="Red">Red</option>

    <option value="Green">Green</option>

    Inspiring
    July 17, 2018

    Hi, thanks so much for your help. Your first example looks the best because there will not be a database. Seems simple enough, will give it a try.