By using JQuery we can create three linked dropdown list. This script is easy to understand and easy to modify.
Tutorial with Demo on using Three linked dropdown list - JQuery
Select Country<br><select name=country id='s1' onchange=ajaxFunction('s1');>
<option value=''>Select One</option>
<?Php
require "config.php";// connection to database
$sql="select distinct country from student5 ";
foreach ($dbo->query($sql) as $row) {
echo "<option value=$row[country]>$row[country]</option>";
}
?>
</select>
Here we used sql distinct command to collect unique country name from the records
for(j=document.myForm.state.options.length-1;j>=0;j--)
{
document.myForm.state.remove(j);
}
var state1=myObject.value.state;
for (i=0;i<myObject.state.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myObject.state[i];
optn.value = myObject.state[i];
document.myForm.state.options.add(optn);
if(optn.value==state1){
document.myForm.state.options[i].selected=true;
}
}
Sajin | 19-06-2014 |
Hi there, First of all, I am very thankful for you for giving this drop down list examples. I am new to ajax, so I encountered some problems... I was using your Three interlinked dropdown listbox using Ajax & PHP. I found a problem that, when the country name has some space in between then the second drop down won't display any values. You have given GBR,IND like that , but when I changed the IND to "IND ABC", the the second drop down for that particular country was not shown. It am struggling with this for the last two days. If you can give me a solution for this then it would be very helpul. Can it be done without using PDO. Because the config file I have with me is not using PDO. So if it can be done only using PDO then it will be very risky for me. Please help me with this Please reply......... Thank you Sajin |
smo | 19-06-2014 |
Add single quote to $row[country] like this '$row[country]' |
Wdjoe | 29-06-2014 |
There is a problem with your code and I can not figure out how to correct it. If there is more than one city name for a state, only the first city name on the list can be chosen. For example... If you choose "USA" then choose "California" the city name that will be displayed is "Los Angeles", but if you try and change that city name for another city name in California such as "San Jose" it reverts back to "Los Angeles". Please help, I have been trying to find a solution for hours. |
smo | 03-07-2014 |
It is fixed now. Check once. Thanks for pointing this bug. |
Jay | 14-04-2015 |
Hey, thanks for your great example! I have a question, how can post additional information with subcat2? For instance Banana Hot description |
smo1234 | 16-04-2015 |
All dropdown list boxes are part of FORM. So any textbox or textarea can be included to carry extra information. |
Jerome | 15-06-2015 |
Hey, thanks for your great tutorial. I have a question, where is the dependence of the dropdown menu created. How can I chance it? I would like to have an input field at the beginning, and then the second dropdown menu have to take options (output from a select-command, dependent on input from the input field. (Sorry for my bad english, I hope it is understandly and correct enough.) |
smo | 15-06-2015 |
Inside zip file ( download ) you will get ajax-dd3ck.php file. In that there are SQL command with where clause to manage the output. You can take the input box data also add into the where condition to get matching output for next dropdown list |
Jerome | 16-06-2015 |
Hey, thanks for your help. Unfortunately, I 'm not so good in web design , and certainly not with AJAX and Javascript. Could you possibly help me in more detail? I'm in an important project and need help. I need an input , and a click on a button, after that the following drop down menus should be filled with select commands from php. I do not know what I have to change .. |
smo1234 | 16-06-2015 |
This can be done, pl open one thread in forum, we will post there. |
JeromeAtom | 17-06-2015 |
Ok. I put my question in this thread: https://www.plus2net.com/forum/topic.php?topic_id=3636 Tanks for your help. |
Rob Ogden | 24-07-2015 |
Great code, thanks. However, I would like to use this with 3 separate database tables, and it only appears to allow for a single column per select, i.e. the option (London, Birmingham) and not an ID. Any easy way of changing the code to allow for the 2 columns to pass through to the city select? |
vasanth | 21-11-2016 |
Dear All, thank u for this it is really helpful for me, but i need after the values are selected from drop down list. how to fetch the selected value into the another database using php echo method the sql variable. kindly suggest my query. |
18-07-2021 | |
I have used the code for my page. However, I want the select boxes to have some initial slecetd values that are retrieved from a database. How can I do that? |