Ajmal Razeel
Ajmal Razeel

Reputation: 1701

Pass a variable with JQuery to the Controller - PHP Codeigniter

I am working on a project using PHP Codeigniter. I want to know how can I sent a variable with JQuery to the controller. Below is my code

<script>
 function Reset_User_Password(id){

  $.post("<?=base_url();?>WebAdmin/Reset_User_Password/id", {id: id}, function(page_response)
  {
  $(".modal-body").html(page_response);
  });

 }
</script>

Here I'm first getting the variable 'id' from function parameter. But when I run this code, it return the string 'id' instead of the actual user id from database. I want to view the user id. Below is my function from the controller..

public function Reset_User_Password()
{
$data['admin_id'] = $this->uri->segment(3);

$this->load->view('admin/user/reset_user_password', $data);
}

Upvotes: 1

Views: 1243

Answers (4)

Gihan Kaveendra
Gihan Kaveendra

Reputation: 19

try below code you should get that id variable from GET method on the controller.

function Reset_User_Password(id){
  $.post("<?=base_url();?>WebAdmin/Reset_User_Password/id="+id
  $(".modal-body").html(page_response);
});

Upvotes: 1

Arun Tyagi
Arun Tyagi

Reputation: 21

You can take help with this code. It is working perfectly at my end

function Reset_User_Password(){

    var currentpwd= document.getElementById("currentpwd").value;

    var newpwd = document.getElementById("newpwd").value;

    var cnfrmnewpwd = document.getElementById("cnfrmnewpwd").value;

    var url = "<?php echo base_url('user/changepwd'); ?>"

    $.ajax({

        type:"POST",

        data:{currentpwd:currentpwd,newpwd:newpwd,cnfrmnewpwd:cnfrmnewpwd},

        url:url,

        success:function(data){ 

             if(data){

             $('#newsleter').html(data); 

             }
        }   

    }); 

}

Upvotes: 1

Anand Jain
Anand Jain

Reputation: 819

Use

$this->input->post('id');

Don't use $this->uri->segment(3); because you are posting id by post method if you want to use $this->uri->segment(3); then do a minior miodification in your function

     $.post("<?=base_url();?>WebAdmin/Reset_User_Password/"+id, function(page_response)
      {
      $(".modal-body").html(page_response);
   });

Upvotes: 2

Saty
Saty

Reputation: 22532

Send data by POST method is not append in url as GET method. So you can't get is using $this->uri->segment().

Instead use

$data['admin_id']=$this->input->post('id');

Upvotes: 2

Related Questions