Muhammad Talha
Muhammad Talha

Reputation: 337

dynamic dependent dropdown using ajax codeigniter

I want to make dropdown where I select the value and it can fetch data from database without submit button. I paste my code here I am student and making project I shall be very thankful to you please help...

My controller is

public function find($id)
{
    $product=$this->user_model->find($id);
    echo "ID ".$product->email;
}

Model is

public function find($id)
{
    $this->db->where('id',$id);
    return $this->db->get('users')->row();
}

View file:

<script type="text/javascript">
    $(document).ready(function() {
        $('#slt').change(function(){
            var country_id = $('#slt').val();
            $.ajax({
                type: 'GET',
                url: "<?php echo base_url('index.php/main/find/')?>"+country_id,
                success: function(result)
                {
                    $('#result').html(result);
                }
            });
        });

    });
</script>
<form>
    <input type="button" value="find" id="btnfind"/>
    <select name="opt" id="slt">
        <option value="1">one</option>
        <option value="2">two</option>
        <option value="3">one</option>
        <option value="4">two</option>
    </select>
    <br/>
    <div id="result"></div>
</form>

Upvotes: 1

Views: 5398

Answers (2)

shar_evonz
shar_evonz

Reputation: 53

Change $('#result').html(result) to $('#result').text(result)

Upvotes: 0

Mayank Vadiya
Mayank Vadiya

Reputation: 1451

<script type="text/javascript">
$(document).ready(function(){

    $('#slt').change(function(){
        var country_id = $('#slt').val();

        $.ajax({
            type:'GET',
            data:country_id,
            url:"<?php echo base_url('index.php/main/find/')?>",
            success:function(result)
            {
                $('#result').html(result);
            }
        });
    });

});

</script>

Upvotes: 1

Related Questions