0

I have this ajax request

$.ajax({
    type : 'POST',
    url : '<?php echo base_url('Home_controller/editUser'); ?>',
    data : { user_id : user_id, name : name, position : position, office : office, password : password, reports_to : reports_to, user_type : user_type, status : status },
    dataType : 'json',
    success : function(data){
        $('#name' + numberId).html(this.name);
        $('#position' + numberId).html(this.position);
        $('#office' + numberId).html(this.office);
        $('#password' + numberId).html(this.password);
        $('#reports_to' + numberId).html(this.reports_to);
        $('#user_type' + numberId).html(this.user_type);
        if(this.status == 1){
            $('#status' + numberId).html("Activated");
        }
        else if(this.status == 0){
            $('#status' + numberId).html("Deactivated");
        }

        $('#edit_user_modal').modal('toggle');
    },
    error : function(errorw){
        alert('Error');
    }
});

This is my editUser method in my Home_controller

public function editUser(){
    $user_id = $this->input->post('user_id');
    $name = $this->input->post('name');
    $position = $this->input->post('position');
    $office = $this->input->post('office');
    $password = $this->input->post('password');
    $reports_to = $this->input->post('reports_to');
    $user_type = $this->input->post('user_type');
    $status = $this->input->post('status');

    $this->home_model->updateUser($user_id, $name, $position, $office, $password, $reports_to, $user_type, $status);

    $user_details = $this->home_model->getUserDetails($user_id);
    echo json_encode($user_details);
}

I used alert on all the values on my data and they are all correct. I also checked the url and it is correct. I wonder where did I go wrong? Can someone please help me.

This is the error jquery-2.2.1.js:9175 POST http://localhost/ppmp/Home_controller/editUser 500 (Internal Server Error). I tried to open the url in the error in a new tab and yes it can be found.

I'm stuck here for hours now. Thanks for the help.

Cronas De Se
  • 331
  • 4
  • 21
  • It's an internal server error. What do the server error logs say? – Quentin Apr 17 '16 at 20:24
  • This is all I got from the access logs `::1 - - [18/Apr/2016:04:16:21 +0800] "POST /ppmp/Home_controller/editUser HTTP/1.1" 500 1460` – Cronas De Se Apr 17 '16 at 20:25
  • 1
    Check inside your PHP Home_controller, editUser method. If request is coming and try to debug it – Mate Apr 17 '16 at 20:33
  • @Mate I edited my post. How can I know if the request is coming coz I can't seem to use `print_r` in my editUser method since I'm using ajax. – Cronas De Se Apr 17 '16 at 20:50
  • 1
    Check http://stackoverflow.com/questions/3209807/how-to-do-error-logging-in-codeigniter-php to write log . – Mate Apr 17 '16 at 21:01
  • Thank you @Mate I found out what my problem was. – Cronas De Se Apr 17 '16 at 21:40
  • 1
    In your config file update your log threshold to 1 and then try running the script again, any error with your php will be included in the log file. From there you should be able to trace where the problem is – RiaanV Apr 17 '16 at 21:43
  • Great! Add your solution as an answer, or delete your question is it a very particular case – Mate Apr 17 '16 at 23:01
  • I'm voting to close this question as off-topic because Problem solved by him self – Abdulla Nilam Apr 18 '16 at 04:01

1 Answers1

0

The problem that i can se, is that in your ajax request you have:

$.ajax({
    type : 'POST',
    url : '<?php echo base_url('Home_controller/editUser'); ?>',
    data : { user_id : user_id, name : name, position : position, office : office, password : password, reports_to : reports_to, user_type : user_type, status : status },
    dataType : 'json',

and in your controller yo have:

$user_id = $this->input->post('user_id');
    $name = $this->input->post('name');
    $position = $this->input->post('position');
    $office = $this->input->post('office');

The problem is that you send a request as a json, but you get the values as a post, you need to change the request from json to normal string.

In the other hand you can get the values in you controller as a:

$values = $this->input->get_post($_GET);

$new_format = jason_decode($values);

and then proccess the result as you need

elddenmedio
  • 1,030
  • 1
  • 8
  • 15