1

I've success get data on controller with php native and show in the view the code like this:

controller

function help(){
    $data['result'] = array("name" => "jason", "age" => 20);
    $this->load->view('profiles', $data); }

and view like this

foreach ($result as $key) {
    echo $key;
}

and result will be show "jason 20"

The question is how to execute like that with ajax without load the url?

2
  • Are you using jQuery? Commented Oct 22, 2016 at 19:25
  • yes i'm using jquery @ProfNandaa Commented Oct 23, 2016 at 1:29

1 Answer 1

1

in the controller return a JSON object

    $this->output->set_content_type('application/json');
    $this->output->set_output(json_encode( $result ));`enter code here

then in the view call the controller with jquery

 `$.ajax({
                    type: "POST",
                    url: "<?php echo base_url('index.php/controller')?>",
                    data: {some_val: val},
                    success: function (data) {
                        $.each(data, function (key, value) {
                         console.log(value.name);
                         console.log(value.age);



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

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.