cascading dropdowns

13 views
Skip to first unread message

Antonio Draven De Santis

unread,
Dec 16, 2014, 10:30:03 AM12/16/14
to codei...@googlegroups.com
hi all, i'm a newbie of codeigniter, my problem is to create a cascading dropdowns populated from mysql db data. In my insert form the first dropdown is populated with the variables exploded of a controller's data array. how can I populate the second dropdown based on the choice of the first?

thanks a lot

Ravi Dhoriya

unread,
Dec 16, 2014, 10:47:36 AM12/16/14
to prof....@gmail.com, codei...@googlegroups.com
Hi,
I'l explain using Country, cities example.

Non Ajax Solution:
On change event of dropdown, you could refresh page with adding parameter of selected value from dropdown. 
<select name="country_id">
    <option value="IN">India</option>
    <option value="US">United States</option>
    <option value="NA">Others</option>
</select>


In controller/method you have to read that get parameter using:
    $country_id=$this->input->get("country_id");
    $cities=$this->m_cities->getCities($country_id);
    $this->view("v_name",array("cities"=>$cities);


jQuery Ajax Solution:

You could POST country_id to some ajax controller (I always create separate controller to handle ajax request), and read response and populate options of another dropdown.

<script>
  $(document).readu(function(){
    $("[name=country_id]").change(function(){
       var country_id=$(this).val(); 
       $.post("http://weburl.com/index.php/controller/method", { country_id : country_id}, 
           function(resp){
               $("[name=city] option").remove();
               $.each(resp.cities,function(city){
                   $("[name=city]").append("<option value='"+city.city_id"'>"+city.city_name+"</option>");
               });
              
           });
    });
  });
</script>

Assuming your http://weburl.com/index.php/controller/method will return following json:

{ "cities" : [ { "city_id" : "ST", "city_name": "Surat"}, { "city_id" : "BLR", "city_name": "Bangalore"} ] }




Thanks,
Ravi Dhoriya

P  "Save a Tree" - Please consider the environment before printing this email.

On 16 December 2014 at 21:00, Antonio Draven De Santis <prof....@gmail.com> wrote:
hi all, i'm a newbie of codeigniter, my problem is to create a cascading dropdowns populated from mysql db data. In my insert form the first dropdown is populated with the variables exploded of a controller's data array. how can I populate the second dropdown based on the choice of the first?

thanks a lot

--
You received this message because you are subscribed to the Google Groups "codeigniter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codeigniter...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ravi Dhoriya

unread,
Dec 16, 2014, 10:49:48 AM12/16/14
to prof....@gmail.com, codei...@googlegroups.com
Sorry, 
Some URL was wrong in previous email:

Non Ajax Solution:
On change event of dropdown, you could refresh page with adding parameter of selected value from dropdown. 
<select name="country_id">
    <option value="IN">India</option>
    <option value="US">United States</option>
    <option value="NA">Others</option>
</select>


In controller/method you have to read that get parameter using:
    $country_id=$this->input->get("country_id");
    $cities=$this->m_cities->getCities($country_id);
    $this->view("v_name",array("cities"=>$cities);


jQuery Ajax Solution:

You could POST country_id to some ajax controller (I always create separate controller to handle ajax request), and read response and populate options of another dropdown.

<script>
  $(document).readu(function(){
    $("[name=country_id]").change(function(){
       var country_id=$(this).val(); 
       $.post("http://weburl.com/index.php/ajax/getCities", { country_id : country_id}, 
           function(resp){
               $("[name=city] option").remove();
               $.each(resp.cities,function(city){
                   $("[name=city]").append("<option value='"+city.city_id"'>"+city.city_name+"</option>");
               });
              
           });
    });
  });
</script>

Assuming your http://weburl.com/index.php/ajax/getCities will return following json:

{ "cities" : [ { "city_id" : "ST", "city_name": "Surat"}, { "city_id" : "BLR", "city_name": "Bangalore"} ] }

Thanks,
Ravi Dhoriya

P  "Save a Tree" - Please consider the environment before printing this email.
Reply all
Reply to author
Forward
0 new messages