pass值的onclick复选框控制器codeigniter控制器、复选框、pass、onclick

2023-09-10 14:55:05 作者:遗忘的岁月

我是新来的CI和我有在以下code问题: 我的控制器显示动态地从数据库国家的所有值,我需要传递的价值onclicking特别chechbox我控制器

我的看法:

 < /头>
    <身体GT;
    < PHP
        的foreach($ countryDetails为$行)
        {
        回声'<输入ID =国家类型=复选框名称=国家级=独一无二的价值=$行向> COUNTRY_ID。'的onclick =>'$行向。 > COUNTRY_NAME'< BR />。
        }
        ?>
    <脚本>
    $('input.unique)。点击(函数(){
    $('input.unique:选中')不是(这个).removeAttr('检查');
    });
    < / SCRIPT>
    <脚本>
 

和我的控制器是

 公共功能指数()
{
$这个 - > view_data ['countryDetails'] = $这个 - > get_county_model-> getCountryDetails();


$这个 - >负载>查看('get_country',$这个 - > view_data);
}
 

解决方案

HTML

 值1<输入类型=复选框级=chkbox值=值1/>
  价值2';输入类型=复选框级=chkbox值=值2/>
  值第3版;输入类型=复选框级=chkbox值=值3/>
  价值4℃;输入类型=复选框级=chkbox值=值4/>
 
控制器向视图传值 一

jQuery的阿贾克斯

  $(文件)。就绪(函数(){

     $(文件)。在('点击','。chkbox',函数(){
      VAR ID = THIS.VALUE;

       $阿贾克斯({
        键入:POST,
        背景:应用/ JSON
        数据:{ID:ID},
        网址:HTTP://localhost/xyz/index.php/controller_name/function_name
        成功:函数(MSG)
        {
            警报(从结果控制');
        }
    })
   });

 });
 

控制器

 公共功能函数名()
 {
   的$ id = $这个 - >输入 - >后期('身份证');
   //现在使用的$ id变量的预期目的。
 }
 

I am new to CI and i am having issues in following code: My Controller shows all the values of countries dynamically from DB, i need to pass value onclicking particular chechbox to my controller

My view:

    </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>

and my Controller is

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


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

解决方案

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');
        }
    })
   });

 });

Controller

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