954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cascading Dropdown list with (AJAX, PHP)

Hi.
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?

Attachments Cascading.zip (3.93KB)
tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

any idea please ...

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

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

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

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.

badbart
Newbie Poster
12 posts since Sep 2007
Reputation Points: 30
Solved Threads: 1
 

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

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

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.

badbart
Newbie Poster
12 posts since Sep 2007
Reputation Points: 30
Solved Threads: 1
 

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

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

Hmmm....

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:

PopulateDepartmentList(XmlHttpObj.responseXML.documentElement);


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

badbart
Newbie Poster
12 posts since Sep 2007
Reputation Points: 30
Solved Threads: 1
 

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

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

Attachments ajax_dropdown_double.zip (218.33KB) php_dorpdown.zip (2.01KB)
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

hi,

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.

<?php

    // 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.

vicky_rawat
Junior Poster
137 posts since Jun 2008
Reputation Points: 28
Solved Threads: 19
 

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

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

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.

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

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

Attachments untitled.JPG 111.69KB
tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 
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.. :)

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

yeah.
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?

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

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

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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 ?

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

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

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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.

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You