CodeIgniter Forums
two drop down list from database - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: two drop down list from database (/showthread.php?tid=12381)

two drop down list from database - El Forum - 10-16-2008

I have two drop down list. The second drop down list will depend on the selected value in the first drop down box.

in normal PHP the code for your view will just be:

$cntry = "Select name from country;";
$sql = mysql_query($cntry);

$country = $_POST['country'];
$query = "SELECT * FROM cities where country = '$country'";

<select name="country">
while($row = mysql_fetch_array($sql))
{ <option>&lt;?=$row['countryname']?&gt;</option> }

<select name="city">
while($result = mysql_fetch_array($query))
{ <option>&lt;?=$result['countryname']?&gt;</option> }

which means you can post the value you selected in the same page and use it to query.

but in codeigniter, you cannot do that. you cannot post data unless you pass it to a model or controller. The requirement of my site is after you select in the country, it will auto populate the second drop down list without going to other pages or clicking on any button.

any help will be appreciated.

two drop down list from database - El Forum - 10-17-2008

To change anything on your screen without changes pages will require javascript.

You would need to make a javascript function that fires anytime the 1st select menu gets changed.

This function would need to send the current select menu value to your php script which would send back the information which you would use to update your 2nd select menu.

two drop down list from database - El Forum - 10-17-2008

You can have an array in javascript and use chain selects (google that) or you can use ajax to post and inject the response into your 2nd select.