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.


In reply to Re: Dynamic Drop down in CGI by dorward
in thread Dynamic Drop down in CGI by d0353101

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.