1

I have an editcategory view which shows details about a category and also a table of category descriptions showing at the bottom of the view.

The category description table has two anchor tags for edit and delete. I am using ajax to pass the id of the description to be edited. A function in the controller needs to be called, which calls the categorydescription model and pulls the record based on the id passed. The controller needs to feed the catDescription array back to the ajax success function.

There is already a hidden div that contains the form for editing the description but the values are set as follows

 <label for="catlang_name">Name</label>
 <input type="text" name="catlang_name" id="catlang_name" class="text ui-widget-content ui-corner-all"  value="<?php echo set_value('catlang_name',$catDescription->catlang_name); ?>"/>

My problem is how I can get the response back in Ajax and assign the

For normally passing the data to the view i could have done the following from within the controller

$data['catDescription']=$this->CategoryModel->getCategoryDescriptionById($id)
$this->load->view('category/categoryEdit', $data,true);

This is the data returned from the model :

Array (
    [0] => Array (
        [catlang_id] => 1
        [catlang_cat_id] => 10
        [catlang_lang_id] => 2
        [catlang_name] => french
        [catlang_description] => test
    )
    [1] => Array (
        [catlang_id] => 2
        [catlang_cat_id] => 10 
        [catlang_lang_id] => 2
        [catlang_name] => english
        [catlang_description] => test
    )
)

Any suggestions would be much appreciated.

cheers

1 Answer 1

5

To pass the array back to the ajax function, you should return it as JSON.

In your controller, rather than calling a view, write this:

$this->output->set_output(json_encode($array));

Which will output the array in JSON, that the ajax function can process. I suppose you're using jQuery for handling the ajax, and if you aren't yet using jQuery, do so.

$.ajax(
    url: '<?php echo site_url() ?>category/categoryEdit.php'
    dataType:'json',
    success: function(data) {
        $.each(data, function(index,value){
            //process your data by index, in example
            $("#catlang_name").val(value.catlang_name);
        });
    }
);
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.