I don't know what is wrong with the following attachment, actually I have two drop down list on my html form, and which is one retrieving the value from MySQL table, and the second is dependent to the first one, and that one also gets its value from another MySQL table Using AJAX technology.

Can anyone help me through this case?

Recommended Answers

All 42 Replies

any idea please ...

Sorry for posting again, it is three days I am doing on the attachment and I don't know what is wrong actually I traced it many times, it should work, but I dont know why not work

Can you be more specific about the problem you're having? Is the first dropdown not loading? Or is it the second that's not loading (depending on the value of the first)? Or is it something else entirely?

I'm currently looking your code over, but I don't really know what to look for, specifically.

the second drop-down not loading (depending on the value of the first).

The problem in your code is that this block of code:

while($department = mysql_fetch_array($resultDepartment)){	
	$xml = $xml . '<faculty name="$department['f_name']">';
	$xml = $xml . '<department id= "$department['dep_id']"> $department['dep_name'] </department>';
	$xml = $xml . '</faculty>';

is commented, thus javascript can't load it.

Also, you load the faculty member's name, instead of their ID, as the "value" of the option in the first drop down. You have it coded to work correctly this way (in the query just above the commented code above), but the queries will be faster if you use the unique id for each member in the where condition of the query.

Thanks for the reply, I remove the comment, but not work, could you please have a test of it on your system ?

That would be very nice of you


I'm also not an AJAX pro, nor am I even a fan, thus I've been unable to get your code to work on my system (I keep getting a Javascript error on this line:


Sorry, I'll have to leave you in the hands of the AJAX experts.

Check these attachments..
These are very usefull for you..

The dropdown works on both php and ajax..
Check out..


The problem was not with AJAX or PHP code, but it was with the xml generation. I have included the modified code. I have added a root element for the XML.


    // This is a very simple data provider for the cascading dropdown
    // example. A *real* data provider would most likely connect to
    // a database, use xslt and implement some level of security.

    Header("Content-type: text/xml"); 
    // get query string params
	$filter = $_GET['filter'];

	//Connect to mysql server
	$link = @mysql_connect("localhost","root","");
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	//Select database
	$db = @mysql_select_db("dropdown");
	if(!$db) {
		die("Unable to select database");
	//Create query of Faculty
	$qryDepartment = "
						SELECT dep_id, dep_name, tbl_faculty.f_id, f_name
						FROM tbl_department, tbl_faculty
						WHERE tbl_department.f_id = tbl_faculty.f_id
						AND f_name = '$filter'
	$resultDepartment = mysql_query($qryDepartment);

    // build xml content for client JavaScript

	$xml = "<mxml>";
	while($department = mysql_fetch_array($resultDepartment)){	
			$fn = $department['f_name'];
			$di = $department['dep_id'];
			$xml = $xml . '<faculty name="'.$fn.'">';
			$xml = $xml . '<department id="'.$di.'">'.$department["dep_name"].'</department>';
			$xml = $xml . '</faculty>';
	$xml = $xml . "</mxml>";	
    // send xml to client
	echo( $xml );

This solves your problem.

Thanks vicky_rawat, for the reply,
BUT it is still not working, I don't know what to do?

Thanks for the attachment,
BUT I don't know why the data in location.sql, can not be inerted into MySQL, and also with open it with notepad, or notepad++, the data is not readable.

Can you please post me the encoding of that, I mean localtion.sql.


Check these attachments..
These are very usefull for you..

The dropdown works on both php and ajax..
Check out..

Thanks vicky_rawat, for the reply,
BUT it is still not working, I don't know what to do?

I tried vicky_rawat's code and it works perfectly fine.. :)

It is all right, JUST I deleted the cookies from the browsers, and now it is working well.

I mean, if you the faculty exist in the first drop-down, and there is no department for it into the tbldepartment, it shows nothing, and then later on if you add departments for that faculty, and you select again that faculty it does not display the related departments, untill you delete the cookies from the history.

I don't know actually why AJAX do like this?

You just copy and paste the content which is in notepad into your mysql query...

When I copy and paste the contents in mysql query, then the data inserted, but not readable.
WHY like this? WHAT I should I do ?

Tel me what mysql version you are using...
Is it phpmyadmin or not...

These what i attatched are finely working...Check out once again...

I am using:
MySQL 5.0.51b
phpMyAdmin 2.11.7

and I used the the import, and also the copy and paste in phpmyadmin, but still the inserted data is not readable?

I checked it many times, and I tried opening the file, with notepad, notepad++, and dream viewer, not readable there.

@Shanti, the sql you have posted (location and state.sql) are corrupted.

Hi.. Nav..
Check this attatchment now...
Tanha Check this anf go through that with previous attatch ments...

Hi.. Nav..
Check this attatchment now...

This is fine!

Thank you Naveen....

Dear Shanti,
it is ok, can u post the location.sql file too ?

Im so sorry tanha..
Presently it is not in my computer...

Anyone able to help me with the following attachments? I'm able to select value from the first drop down list. But the second drop down list value would be that of the first drop down list. Like a repeat. I'm new at this, not sure how do I get value from another table from the database for the second drop down list. I'm trying to get card_name from the card table for the second dropdown list. Been looking at it for days. And stucked. Hope someone can help?

Hello spazzlady...
I have seen your attachment , in that ,i think your issuer table is wrong.
Means , issuer table has no subcategories...Thats why repetition is done...
So tel me from which table you have to get values to the second drop down...

Thanks to all of your guidance (this was my first time playing with AJAX), I've come up with a more configurable version of the cascading dropdown list code that my fellow Ajax-noobs might find helpful.

Here is what's in the attached zip:
AjaxCode.js - this is essentially the same as the other javascript files that have been posted here with a few minor tweaks.
data_settings.php - this is a file that's included in the php pages that query the database. It contains the hostname, username, password, and database name for the database that will house the tables, creates the database connection and selects the database.
index.php - this is a more dynamic main page that builds itself using two tables. The first table, ajax_data, contains all the data to be placed in the dropdowns. The second table, ajax_labels, contains the descriptions labels for each dropdown and more importantly, an id field that determines the value passed in the onchange event to load the dependent dropdown options.
tables.sql - this is the sql to build and fill the generic tables used by the code. You can add any number of dropdowns by adding records to ajax_labels and ajax_data with the proper corresponding id (in the label table) and parent & level (in the ajax_data table).
xml_data_provider.php - this is essentially the same file as was attached in the first post, with the required tweaks to make it work.

One question for you AJAX experts: Do you have any idea why using an ampersand in the text description (the book title in my attached example) causes an error? To duplicate the error, just replace the word "and" with "&" in any of the titles.
For example, this causes an error:

<mxml><parent_level name="1"><child_level id="9">The Hitchhiker`s Guide to the Galaxy</child_level></parent_level><parent_level name="1"><child_level id="10">The Restaurant at the End of the Universe</child_level></parent_level><parent_level name="1"><child_level id="11">Life, the Universe, [B]&[/B] Everything</child_level></parent_level><parent_level name="1"><child_level id="12">So Long and Thanks for all the Fish</child_level></parent_level><parent_level name="1"><child_level id="13">Mostly Harmless</child_level></parent_level><parent_level name="1"><child_level id="14">Dirk Gently`s Holisitic Detective Agency</child_level></parent_level><parent_level name="1"><child_level id="15">The Salmon of Doubt</child_level></parent_level><parent_level name="1"><child_level id="16">Last Chance to See</child_level></parent_level></mxml>

I just discovered that the multi-level (beyond 2 dropdowns) logic is flawed. I'll post the fix as soon as I figure out where the problem is.

Be a part of the DaniWeb community

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