i am able to populate data in chain select drop down list but the problem is when page is refreshing selected data is lost how to fix that .. here is the code..

<?php
require_once 'include/config.php';
require_once 'include/opendb.php';
?>
<?php
if(isset($_POST['model'])){ //This is for the third and final drop down menu. It allows you to submit and go to new page.
    $link="index.php";
}else{ //This is for the third and final drop down menu. It allows you to submit and go to new page.
    $link="#"; 
}
?>
<form method="post" action="<?php echo $link;?>" name="admin"> //Straight Forward
<select name="value1" onChange="this.form.submit();"> // 1Ste Drop Down Menu Starts here
<?php
$result = mysql_query("SELECT * from table");
echo("<option value=''>---Select---</option>");
if(mysql_num_rows($result)) {
while($row = mysql_fetch_row($result))
{
echo("<option value='$row[0]'>$row[0]</option>");
}
} else {
echo("<option value=''>No</option>");
}
echo "</select>";
?>
<?php
if(isset($_POST['value1'])){ // Checks if 1Ste Drop Down menu has a value.
$value1=$_POST['value1']; 
?>
<select name="value1" onChange="this.form.submit();"> //2nd Drop Down Menu Starts here
<?php
$result = mysql_query("SELECT * from table where table='$value1'");
echo("<option value=''>---Select---</option>");
if(mysql_num_rows($result)) {
while($row = mysql_fetch_row($result))
{
echo("<option value='$row[0]'>$row[0]</option>");
}
} else {
echo("<option value=''>No Data</option>");
}
echo "</select>";    
?>  

<?php
}else{
    echo "\n";
}
?>
<?php
if(isset($_POST['value2'])){ // Checks if 2nd Drop Down menu has a value.
$value2=$_POST['value2'];
?>
    <select name="value3"> //3nd Drop Down Menu Starts here
<?php
$result = mysql_query("SELECT * from table where table='$value2'");
echo("<option value=''>---Select---</option>");
if(mysql_num_rows($result)) {
while($row = mysql_fetch_row($result))
{
echo("<option value='$row[0]'>$row[0]</option>");
}
} else {
echo("<option value=''>No Data</option>");
}
echo "</select>";    
?>
<?php
}else{
    echo "\n";
}
?>
<input type='submit' value='Select'>

Recommended Answers

All 7 Replies

there are two ways to do that:-
1) when you submit a form,send its options index and then on submitting form,read from url and set that as selected value

country.options.selectedIndex = index_of_selected_country;

and so on.

Although this is not the recommended solution.

2) The best way to achieve this is by ajax call.On change of value of selectbox,make an ajax call to another php page that will set the value of second box based on first one.
This is the most recommended option for this.

Member Avatar for diafol

Agreed, ajax is probably best way to deal with linked selects. Reloading page on every select event is extremely tedious.

Thanks a lot for your suggestion IIM actually i am not getting a clearer picture like how to implement it,actually i dont want to call ajax coz i need to refresh the page on every selection here is my code in which im trying to implement it please make some modification your help would be highly appreciated..thanks a ton

<?php
require_once 'dbcon.php';
?>

<?php

if(isset($_POST['city'])){ //This is for the third and final drop down menu. It allows you to submit and go to new page.
    $link="index.php";
}else{ //This is for the third and final drop down menu. It allows you to submit and go to new page.
    $link="#"; 
}
?>

<form method="post" action="<?php echo $link;?>" name="admin"> 
<select name="value1" onChange="this.form.submit();"> // 1Ste Drop Down Menu Starts here

<?php
$result = mysql_query("SELECT distinct(bank) from ifsc_details");
echo("<option value=''>---Select Bank Name---</option>");
if(mysql_num_rows($result)) {
while($row = mysql_fetch_row($result))
{
echo("<option value='$row[0]'>$row[0]</option>");
}
} else {
echo("<option value=''>No</option>");
}
echo "</select>"; 

?>

<?php
if(isset($_POST['value1'])){ // Checks if 1Ste Drop Down menu has a value.
$value1=$_POST['value1']; 
?>
<select name="value2" onChange="this.form.submit();"> //2nd Drop Down Menu Starts here
<?php
$result = mysql_query("SELECT distinct(state) from ifsc_details where bank='$value1'");
echo("<option value=''>---Select state---</option>");
if(mysql_num_rows($result)) {
while($row = mysql_fetch_row($result))
{
echo("<option value='$row[0]'>$row[0]</option>");
}
} else {
echo("<option value=''>No Data</option>");
}
echo "</select>";    
?>  

<?php
}else{
    echo "\n";
}
?>
<?php
if(isset($_POST['value2'])){ // Checks if 2nd Drop Down menu has a value.
$value2=$_POST['value2'];
?>
    <select name="value3"> //3nd Drop Down Menu Starts here
<?php
$result = mysql_query("SELECT distinct(district) from ifsc_details where state='$value2'");
echo("<option value=''>---Select---</option>");
if(mysql_num_rows($result)) {
while($row = mysql_fetch_row($result))
{
echo("<option value='$row[0]'>$row[0]</option>");
}
} else {
echo("<option value=''>No Data</option>");
}
echo "</select>";    
?>
<?php
}else{
    echo "\n";
}
?>
<input type='submit' value='Select'>

how to use drop down list in php n addition numbr of value using drop down list how can i do this

Member Avatar for diafol

Perhaps better to start a new thread on this. Have you searched Google and this forum? This topic has been covered a number of times in the past. In addition, you provide very little info - show us your code so far.

Dear friends ,
I need a help from for chain selected box how to do it? .
how to take the selected value from dropdown box selected by the user?
Based on the selected value filter the value from the second dropdown box
simillarly, third dropdown box and fourth dropdown box.How to do it?

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.