Newrain Home
 | Home | Contact | Link To Us | Web Design |

 Newrain.com, where website design and customer service go hand in hand!
Check out our programming tips for Active Server Pages!

Go to Web Design Page


Using OnChange to Fill a Drop-Down Menu

From time to time it becomes necessary to have a drop-down menu fill dynamically based on what a user chooses from another drop-down menu.  This can be very useful when using a form to add records to a table in a database that includes fields linked to another table, in a one to many relationship.  I.E. A program type table called "Programs" may have another table called "Code" that is linked to it via the "ProgType" field (the primary key of the "Programs" table).  But how do we set this up?

OnChange:

The best way I've found to do this is via an "onChange" event using JavaScript.  The calling "select" statement will contain the reference to a function utilizing this event:

onChange="getCode(this)"

Then a JavaScript function (placed between the tags) called "getCode()" then passes the value selected via the ProgType menu back to the same page via a window.location method:

window.location = "onchange.asp?ProgType=" + ProgType

Within the same page you will need to add a querystring that passes the ProgType value into a "ProgType" variable.  This value is then used to retrieve the Code values and fills the Code drop-down menu.

Note:  make sure you test for a value in the CodeType variable and if it isn't blank or null, use it to retrieve the "selected" menu item that will appear in the ProgType down menu.

Sample
Ranking:

What do you think?

 


Home |  Contact |  Link to Us |  Login |  Website Design |  Web Hosting |
Domain Registration |  Consulting |  Database Design |  Customer Feedback |  Awards |
© Newrain Designs