d0353101 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am having 2 dropdowns on my page. The values in both the drop downs have to be populated through DataBase.

Problem : I dont know how to populate the second Dropdown automatically if items in first dropdown changes.

First DropDown : City names are getting populated as per user's country.

Second Dropdown : Market names to be populated here when a city name is selected in first drop down.

Please show me some way to populate the Market names as per selected city name in first drop down.
I am using PERL. ( And I am not Using AJAX).

I understand that I will have to use Java script. And I think I will have to use OnChange event. But after giving it a try, I could not do it.

Many thanks for your help.

Vivek Jain

Replies are listed 'Best First'.
Re: Dynamic Drop down in CGI
by dorward (Curate) on Jun 09, 2009 at 14:21 UTC

    Building with the principles of progressive enhancement, the first thing to do is to write it so it works without JavaScript.

    There are two approaches you could take. Both will start with a select element containing the list of cities. Then you either…

    1. Have a simple text input in which the user can enter the data free form.
    2. Have a submit button and let the user go to a new page which gives them the next set of choices.

    Then you can enhance with JavaScript.

    If you went with the free form text input, remove it using JS.

    Generate something to indicate that more options will be available once the first select has items picked. A disabled select element for instance.

    When the first select has an item picked, populate the second one and stop it being disabled. You will need to get the data from somewhere. This could be included in the page at load time, or requested from the server (using an Ajax technique) on demand. The first option will be more responsible, but will have a heavier inital load. The decision should be based on the size of your database.

    Getting the data into a form that is friendly to JavaScript is a good job for JSON::Any.

    I'll leave out the specifics of writing the JavaScript to deal with that data, this is PerlMonks, after all.

Re: Dynamic Drop down in CGI
by Anonymous Monk on Jun 09, 2009 at 13:19 UTC
    I am not Using AJAX.

    You will have to use J (JavaScript)

    A reply falls below the community's threshold of quality. You may see it by logging in.