3
<script type="text/javascript">
    $(document).ready(function () {
        $("#select-dept").change(function () {
            var id = $("#select-dept").val();
            $.ajax({
                type: "POST",
                url: "<?=base_url()?>.index.php/sms/get_dept_employee",
                //url: baseurl + 'sms/get_dept_employee',
                data: "id",
                dataType = "json",
                cache: "false",
                success: function (emp_list) {
                    $("#dept-emp").html(emp_list);
                }
            });
        });
    });
</script>

I am unable to send view data to controller function In view their is select box with departmenr values from mysql database

<select class="select-dept" id="select-dept" name="select-dept">
    <option>Select Department</option>
    <?foreach ($departments as $dt):?>
        <option value="<?=$dt['id']?>">
            <?=$dt[ 'name']?>
        </option>
    <?endforeach;?>
</select>

i need to get refresh when user select department and that need to call controller function get_dept_employee And need to display datagrid of employee list

0

4 Answers 4

13

you need to send data option as object

try this

 .....
 url: "<?=base_url()?>.index.php/sms/get_dept_employee",
 data: {"id":id},
 dataType:"json",
 ....

and get the posted value as id in your controller..

$id=$this->input->post('id');
....
Sign up to request clarification or add additional context in comments.

Comments

2
var id = $("#select-dept").val();     

 data:"id", //Here you are sending string as id

should be

     data:id, //No double quotes surrounded 

Comments

0

Try:

data: {'id':$(this).val()},

Comments

0

i saw few errors

use data: {'id':id}, instead of data: "id",

use dataType:"json", instead of dataType="json",

use cache:false instead of cache: "false",

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

        var base_url = "<?php echo $this->config->item('base_url'); ?>";

        $("#select-dept").change(function () {

            var id = $(this).val();
            $.ajax({
                type: "POST",
                url: base_url+"index.php/sms/get_dept_employee";
                data: {'id':id},
                dataType:"json",
                cache: false
                success: function (emp_list) {
                    $("#dept-emp").html(emp_list);
                }
            });
        });
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.