Question: i have country dropdown list and region dropdown list. Region drop downlist should be populated with values based on the chosen country using jQuery. I googled and tried to solve this problem, but i failed, i couldn't do it. Any simple steps that would help me achieve this?
Here is my snippet:
Basically first page load will populate the drodown lists from db and also store the list in sessions so list can re-used at application level as long as the session is active.
Default countryID and regionID are put in session called $_SESSION['regionID'] and $_SESSION['countryID']
if(!isset($_SESSION['countryList']))
{
$_SESSION['countryList'] = array();
}
if(!isset($_SESSION['regionList']))
{
$_SESSION['regionList'] = array();
}
.
.
.
<form action="index.php" method="GET">
<label for="lbl_country" id="countrylabel">Country</label>
<select name="country" id="countrylist" onchange="getRegions(<?php $_SESSION['regionID']; ?>)">
<?php
//fetch country list from database and store it in session
if(isset($_SESSION['countryList']) && !empty($_SESSION['countryList']))
{
foreach ($_SESSION['countryList'] as $val)
{
echo '<option value="'.$val['countryID'].'"';
if($_SESSION['countryID'] == $val['countryID'])
{
echo 'selected';
}
echo ">".$val['countryName']."</option>'";
}
}
else //means the session is not set yet-or-session is empty. Either case, we have to fetch
{
$query = "SELECT countryID, countryName FROM Country ORDER BY countryID ASC";
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results))
{
echo '<option value="'.$row['countryID'].'"';
if($_SESSION['countryID'] == $row['countryID'])
{
echo 'selected';
}
echo ">".$row['countryName']."</option>'";
$_SESSION['countryList'][] = $row;
}
}
?>
</select>
<label for="lbl_region" id="regionlabel">Region</label>
<select name="region" id="regionlist">
<?php
if(isset($_SESSION['regionList']) && !empty($_SESSION['regionList']))
{
foreach ($_SESSION['regionList'] as $val)
{
echo '<option value="'.$val['regionID'].'"';
if($_SESSION['regionID'] == $val['regionID'])
{
echo 'selected';
}
echo ">".$val['regionName']."</option>'";
}
}
else
{
$query = "SELECT regionID, countryID, regionName FROM Region WHERE countryID =".$_SESSION['countryID']." ORDER BY regionID ASC";
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results))
{
echo '<option value="'.$row['regionID'].'"';
if($_SESSION['regionID'] == $row['regionID'])
{
echo 'selected';
}
echo ">".$row['regionName']."</option>'";
//everytime you go to a loop, make sure to put the result in the session
$_SESSION['regionList'][] = $row;
}
}
?>
</select>
</form>
Lets take an easy example, I'm using this and it works perfectly fine.
This is the country dropdown:
This is the region dropdown:
Now make a seperate file named crlist.js and include it in the page having above code like this:
code for crlist.js:
Now make a seperate file named crlist.php.
Code for crlist.php:
Now add following script on the page having dropdowns:
This is my own script, and i've assumed that you have created country and region tables. But you need to tweak the queries and above code according to your db structure.
Hope this helps.