Can anyone help. Im trying to create 2 drop down lists (using the select tag). Based on the choice of the first drop down list the second list is populated.


Recommended Answers

All 4 Replies

You need to generate a piece of dynamic html - ie. produced on the fly, often referred to as AJAX.

The first <select> needs an OnChange property, which calls a JavaScript function,
which, in turn, performs am HTML "GET" to call another script on your server,
which returns data to make up the contents of the next <select>,
which must reside inside a DIV of its own, so you can replace its innerHTML.

Look up getHTTPObject for starters.

This instantiates an HTTP object, with which you
"open" the "GET" on your remote script,
"send" to obtain the response, and
define an "onreadystatechange" property to call another function to do stuff with your result... when it gets back to you.

Its not as hard as it sounds. Debugging a mistake; now that's hard - and can take you hours !

Some notes. I use "document.getElementById" to obtain existing data from the form that <select> resides in - to send parameters to my remote script.
Also, if I need to populate more than 1 DIV with my resulting data, I concatenate each part with a pipe ("|"), and then "split" it in my statechange function into an array and populate each DIV (hard coded names) with subsequent array elements.

Bit terse, but I hope that helps.

@vibha-nice code....and that too with quite well explanation...

Thanks all.

Being that Im only just starting to learn php (and python) AJAX, JS all seems a bit daunting.

I have, however, come across the following code which is (pretty much) what Im looking for.
The only thing is, is that I need to add an additional 2 drop lists which are populated based on the select of the "Platform" list.

The code I have so far is :

<html dir="ltr" lang="en">
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
	<style type="text/css">
	<script type="text/javascript">
		function populate(o)
			var mitems=new Array();
			for(var i=0;i<cur.length;i++)
<form action="" method="get">
	<label for="or">Vendor </label>

	<select name="or" id="or" onchange="populate(this)">
		<option value="Example1">Example1</option>
		<option value="Example2">Example2</option>

	<label for="de">Platform </label>
	<select name="de" id="de">
        <label for="de">Type 1 </label>
	<select name="de" id="de">

        <label for="de">Type 2 </label>
	<select name="de" id="de">

<input type="submit" value="Show me" />
<p>No Output..</p> 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.