将值Onclick复选框传递给控制器​​codeigniter

时间:2014-01-03 07:54:51

标签: javascript php jquery ajax codeigniter

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

我的观点:

    </head>
    <body>
    <?php 
        foreach ( $countryDetails as $row ) 
        {
        echo '<input id="country" type="checkbox" name="country" class="unique" value="'.$row->country_id.'" onclick="">'.$row->country_name.'<br/>';
        }
        ?>
    <script>
    $('input.unique').click(function() {
    $('input.unique:checked').not(this).removeAttr('checked');
    });
    </script>
    <script>

我的控制器是

public function index()
{       
$this->view_data['countryDetails'] = $this->get_county_model->getCountryDetails();


$this->load->view('get_country' , $this->view_data );
}

1 个答案:

答案 0 :(得分:0)

HTML

  value 1<input type="checkbox" class="chkbox" value="value1"/>
  value 2<input type="checkbox" class="chkbox" value="value2"/> 
  value 3<input type="checkbox" class="chkbox" value="value3"/>
  value 4<input type="checkbox" class="chkbox" value="value4"/>

Jquery Ajax

 $(document).ready(function(){

     $(document).on('click','.chkbox',function(){
      var id=this.value;

       $.ajax({
        type: "POST",
        context: "application/json",
        data: {id:id},
        url: "http://localhost/xyz/index.php/controller_name/function_name",
        success: function(msg) 
        {
            alert('result from controller');
        }
    })
   });

 });

控制器

 public function function_name()
 {
   $id=$this->input->post('id');
   //now use the $id variable for the desired purpose.
 }