无法将值传递给控制器​​onclick复选框codeigniter

时间:2014-01-03 11:47:01

标签: php jquery ajax codeigniter codeigniter-2

我是CI新手,我在以下代码中遇到问题:我的控制器动态显示来自数据库的国家/地区的所有值,我需要将特定chechbox的值传递给我的控制器

我的观点代码是:

<?php  foreach ( $countryDetails as $row ) {
    echo '<input id="country" type="checkbox" name="country" class="unique" value="'.$row->country_id.'">'.$row->country_name.'<br/>';
}?>
<script>
    $('input.unique').click(function() {
        $('input.unique:checked').not(this).removeAttr('checked');
    });
    $(document).ready(function(){
        $(document).on('click','.unique',function(){
            var id=this.value;
            $.ajax({
                type: "POST",
                context: "application/json",
                data: {id:id},
                url: "http://localhost/index.php/fetch_country/index",
                success: function(msg){
                    alert('$id ');
                }
            });
        });
    });
</script>

控制器

public function index() {
    $this->view_data['countryDetails'] = $this->get_county_model->getCountryDetails();
    $this->load->view('get_country' , $this->view_data );
}
public function fetch() {
    $id=$this->input->post('id');
    echo ('$id');
}

1 个答案:

答案 0 :(得分:0)

    $(document).on('click','.unique',function(){
        var id=$(this).val();
        $.ajax({
            type: "POST",
            context: "application/json",
            data: {id:id},
            url: "http://localhost/index.php/fetch_country/index",
            success: function(msg){
                alert(msg);
            }
        });
    });

public function fetch() {
    $id=$this->input->post('id');
    echo $id;
}