1

as i am new to codeigniter and ajax please can any one do favor for me.I am sending array from my controller to ajax function but how can i access that array element.I am posting here my code. Thanks in advance.

public function index()
{
    $Modules = $this->General_model->AllMoudules();
    $data['result'] = $this->printTree($Modules);
    $output = array(
    'html'=>$this->load->view("Add_user",$data),
    'data' => $data
    );
    echo json_encode($output);
}

and ajax function

$("#user").on("click",function(){      
var url=static_url+'/User';
$.ajax({
        type:"POST",
        url:url,

        success:function(output)
        {
           $(output).each( function (index, o) {
           alert (o.data);

         });
        }

  });

});

5
  • And what isn't working? When you use your browser's debugging tools, what is the value of output in the success function? Commented Jun 21, 2017 at 13:08
  • just check first your output by; console.log(output). in console by inspecting element. Then you can understand this easily how to function on data. Commented Jun 21, 2017 at 13:08
  • you can also put die('hello'); in controller for checking that this is calling or not. Commented Jun 21, 2017 at 13:10
  • thanks for reply as you say to check console.log(output) i did that whole html code is appearing in it because i am sending it in array named output but i want to access first array element and second array element seperatly Commented Jun 21, 2017 at 13:13
  • add header('Content-type: application/json') to your controller's method. Commented Jun 21, 2017 at 14:13

4 Answers 4

2

controller:

  public function index()
        {
            $Modules = $this->General_model->AllMoudules();
            $data['result'] = $this->printTree($Modules);
            $output = array(
            'html'=>$this->load->view("Add_user",$data,true),
            'data' => $data
            );
            echo json_encode($output);
        }

**Javascript:**

$("#user").on("click",function(){      
var url=static_url+'/User';
$.ajax({
        type:"POST",
        url:url,

        success:function(output)
        {
          console.log(output.data);
          console.log(output.html);

         });
        }

  });

Try this.

Sign up to request clarification or add additional context in comments.

Comments

1

To load view in the string you have add third parameter to load view as below:

$output = array(
    'html'=>$this->load->view("Add_user", $data, true),
    'data' => $data
);

To access your ajax response try the below code

success:function(output)
{
    console.log(output.html);
}

Comments

1

Just get the length of array using array.length. Then for loop it

Comments

0

You should use ajax datatype as json like this

$.ajax({
        type:"POST",
        url:url,
        dataType: 'json',
        success: function (data) {
            alert(data.key);
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.