Anandhu Nadesh
Anandhu Nadesh

Reputation: 672

Ajax success function not working after calling controller

I have an ajax call from view to controller with some data passing to controller. Then the data is passed to the model and it is inserted in the db. It all works correctly. But i have some code in my ajax call success function, which is not working. I have no idea how to make it work.

My ajax call from view

$.ajax({
        type:"post",
        dataType : "json",
        url: "<?php echo base_url() ?>att_controller/updt",
        data:{name:nam, age:ag, ids:id},
        success: function()
        {
            //code that i need to make work
        }
    })

my controller

public function updt(){
        $data['name']=$this->input->post('name');
        $data['age']=$this->input->post('age');
        $data['id']=$this->input->post('ids');
        $this->load->model("Att_model");
        $this->Att_model->updt($data);


    }

my model

public function updt($data)
    {

        $this->load->database();
        $name=$data['name'];
        $age=$data['age'];
        $id=$data['id'];
        $this->db->query("update student set name='$name', age='$age' where id='$id'");
    }

need help to figure this out. i'm new to codeigniter

Upvotes: 0

Views: 774

Answers (1)

Alex Mac
Alex Mac

Reputation: 2993

You forget to pass response in both the file controller and model. IN ajax call you defined dataType:'JSON' so in controller you need to convert your data into json format and pass it to view.

public function updt(){
        $data['name']=$this->input->post('name');
        $data['age']=$this->input->post('age');
        $data['id']=$this->input->post('ids');
        $this->load->model("Att_model");
        $result['data'] = $this->Att_model->updt($data);
        echo json_encode($result);
}

Also your model function will look like below.

public function updt($data)
{

        $this->load->database();
        $name=$data['name'];
        $age=$data['age'];
        $id=$data['id'];
        $query = $this->db->query("update student set name='$name', age='$age' where id='$id'");
        return $query->result();
}

Please check this and let me know if it not works for you.

Upvotes: 1

Related Questions