dsm42026
2018-07-17 13:04
浏览 46

下拉中的Onchange事件触发并在codeigniter的控制器中获取值

I need some help in ajax when onchange event fire in dropdown I want the value in controller but I did not get it.

Here is my view code:

VIEW:

<select name="user_role" id="user_role" onchange="calculate(this)">
    <option>--Select User--</option>  

    <?php foreach ($roll_get as $roll) { ?>

    <option value="<?php echo $roll->user_id; ?>" id="<?php echo $roll->user_id; ?>">
        <?php echo $roll->full_name; ?>
    </option>

    <?php } ?>

</select><br>

Below is my AJAX code:

function calculate(id)
{
    var id=id;

    $.ajax({  
        type:'POST',
        url:'welcome/user_manager',
        data:{'id':id},
        success:function(data){}
    });
}

This is my CONTROLLER:

public function user_manager() {
    $id=$this->input->post('id',true);
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjuebin2519 2018-07-17 13:08
    已采纳

    Hope this will help you :

    Your js calculate method should be like this :

    function calculate(obj) 
      {
        var id = $(obj).val();
        console.log(id);
        $.ajax({
                type:'POST',
                url:"<?php echo site_url('welcome/user_manager');?>",
                data:{'id':id},
                success:function(data){
                }
            });
      }
    

    Controller's user_manager method should be like this :

    public function user_manager() 
    {
       $id = $this->input->post('id',true);
       echo $id;
       exit;
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题