SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP Ajax Double Dropdown List Download Script

Two related drop down list by using Ajax & PHP

Please read the basic drop down list box control tutorial. We will try to develop similar functionality by using Ajax (asynchronous JavaScript and XML) and PHP. The biggest advantage of using Ajax here is we need not reload the page again. With more form components ( if already filled by visitor ) we have to re-fill all the data once the page reloads. Now without sending all the information back to server we can send only cat_id of selected first category and then we will manage the second list based on cat_id value.

The detail tutorial on how to start with Ajax or how it works along with PHP is not explained here. This demo script you can download the sample code along with table structures data at the end of this file in zip format.

You can see the demo here and check the other related articles on this related drop down list at the end of this page.

We have used one server side script written in PHP. You can see the code of this file in your zip file

Demo of two dropdown list using Ajax & PHP

New script with multiple selection drop down list box is used. This part is added in your zip file.

Demo with multiple selection dropdown list

Three drop down list using Ajax

download script

Number of User Comments : 10

after selecting item from combobox i.e category, population of data in second combobox is not working. im getting empty small box.

any help would be appreciated.
Yusrizal Siregar22-11-2013
I'm not using 2 option, but 1 option and 1 textarea, need editing on dd.php and main.php. Work well

Nice share!
I've struggled to integrate this into my website for days - - can someone help me? I believe this works, but just can't get it through my thick skull... Thanks for everything!
Thank you for your script. I got it to work but is it possible for the second select option to return both the name and an id from the mysql table ?

For example to return information like this ? Thank you in advance for your help.
Yes , we can use like that and many time we use option and string different data. For that in your sql you can collect both columns and format accordingly. For example we can display student name but keep student ID in option value.

Thank you for your reply,

my mysql dd script is like this

$str=$str . "$r_p[lastname] $r_p[firstname] $r_p[id]".",";

it outputs this :

"John Smith 1, Joe Black 2,"

This works perfectly,

however, I would like to have in the

Since I don't know how to do it, in my passing script I then use

$subcat = filter_var($subcat, FILTER_SANITIZE_NUMBER_INT);

this removes everything and keeps the ID, as you can see it's not a good solution.

Thanks for pointing out that. I can understand your requirement. The script will be modified accordingly. Give me few hours.
Download fresh copy of script now.See the changes. Now for 2nd dropdown you should get subcat_id as value and subcategory as text part of the dropdown list.Submit the form and see...
Thanks for your reply, i don't know if i downloaded the right one, but there are two things i noticed :

1. In dd.php there is a mistake :

$sql="select subcategory,subcat_id from subcategory where cat_id='$cat_id'";

Should be

$sql="select subcategory,cat_id from subcategory where cat_id='$cat_id'";

because of your sql_dump file :

CREATE TABLE `subcategory` (

`cat_id` int(2) NOT NULL default '0',

`subcategory` varchar(25) NOT NULL default ''


I tried running your script, but I get :

$fname =

$cat = 3

$subcat = undefined

thanks, it is fixed now. SQL is correct. The dump file is updated with subcat_id column. Download a fresh copy. Let us know if you are facing any problem.
Post Comment This is for short comments only. Use the forum for more discussions.
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer