Welcome Guest, Not a member yet? Register   Sign In
Update Dropdown Options based on User Input in View
#1

Hi, I want to update the <option> of dropdown input from a database, based on user input. Here my table structure:

I have two tables namely 'district' and 'city'

STRUCTURE OF 'district' TABLE:

id              name               active
1                ABC                    1
2                XYZ                    2


STRUCTURE OF 'city' TABLE:

id              name[b]               district               active[/b]
1                DEFG                    1                         1
2                IJKL                      1                         1
2                MNOP                   2                         1
2                QRST                    2                         1

I want to update 'city' select <option> in view to be updated based on the <option> selected for 'district' <select> dropdown. Here is method responsible for view.

CODE OF METHOD
Code:
public function edit($id = NULL)
{
    // Get data for dropdown
    $this->data['district'] = $this->district_m->get();
    $this->data['city'] = $this->city_m->get();

    $this->template('data/add', $this->data);
}

CODE OF VIEW

Code:
<select name="district" required>
    <option value='' <?php if($district == '0'){ echo 'selected';} ?>>--Select--</option>
    <?php foreach($district as $row) { ?>
        <option value="<?=$row->id?>" <?php if($row->id == $district->district){ echo 'selected';} ?>><?=$row->name?></option>
    <?php } ?>
</select>
<select name="city" required>
    <option value='' <?php if($city == '0'){ echo 'selected';} ?>>--Select--</option>
    <?php foreach($city as $row) { ?>
        <option value="<?=$row->id?>" <?php if($row->id == $district->city){ echo 'selected';} ?>><?=$row->name?></option>
    <?php } ?>
</select>

In the above code the data data from database has been updated in <select> dropdown options for both district and city. But I want cities name based on selected district to be updated in city <select> dropdown options.

PLEASE HELP
Reply
#2

(This post was last modified: 03-17-2016, 04:08 AM by InsiteFX.)

You will need to use javascript to do this, here is the link to one.

Update Dropdown Listbox

PHP Way:

PHP Dropdown Listbox
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(03-17-2016, 04:06 AM)InsiteFX Wrote: You will need to use javascript to do this, here is the link to one.

Update Dropdown Listbox

PHP Way:

PHP Dropdown Listbox

Thanks for reply, I have tried to get the same through jquery, but I didn' get the result here is my code:

Here is my View:
Code:
<option value='' selected>--Select--</option>
    <option value="4">Bilaspur</option>
    <option value="3">Kinnaur</option>
    <option value="6">Mandi</option>
    <option value="1">Shimla</option>
    <option value="2">Solan</option>
    <option value="5">Una</option>
</select>
<select name="city" id="city"></select>

JAVASCRIPT:
Code:
<script>
    $('#district').change(function(){
        $.getJSON(
            '"<?php echo base_url(); ?>check/data',
            'district='+$('#district').val(),
            function(result){
                $('#city').empty();
                alert(result);
                $.each(result.result, function(){
                    $('#city').append('<option>'+this['city']+'</option>');
                });
            }
        );
    });
</script>

CONTROLLER:
Code:
class Check extends Admin_Controller {

    function __construct()
    {
        parent::__construct();

        $this->load->model('check_m');
    }

    public function data($check){

        $data = $this->check_m->get_city(array('district' => $check), TRUE);
        echo json_encode(array('result'=>$data));

    }
}

MODEL:
Code:
public function get_city($where){
    $this->db->where($where);
    return $this->db->get('citys');
}


Kindly help me? and tell me where I am doing wrong.


Thanks.
Reply
#4

Should be:

Code:
<select name="city" id="city"></select>
   <option value='' selected>--Select--</option>
   <option value="4">Bilaspur</option>
   <option value="3">Kinnaur</option>
   <option value="6">Mandi</option>
   <option value="1">Shimla</option>
   <option value="2">Solan</option>
   <option value="5">Una</option>
</select>

You also need to create the second select listbox.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Thank you InsiteFX. My problem get solved. Thanks for your support.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB