Welcome Guest, Not a member yet? Register   Sign In
Quick JS Question
#1

[eluser]veritascs[/eluser]
I'm newish to javascript and having a little trouble. I got the AJAX part down, I just don't know how to handle the returned array in JS (or maybe I'm passing it incorrectly from CI).

Anyway, here is my JS (using jQuery):
Code:
function getCat(obj) {
    var catID = obj[obj.selectedIndex].value;
    
    $.post("categories/getCat", { category_id: catID },
        function(data){
           formObject = document.forms['modify_category'];
           formObject.elements['category_name'].value = data["category_name"];//THIS IS WHERE IM SUCK
   });
}

My controller:
Code:
function getCat() {
        $category = $this->category_model->getCategoryInfo($this->input->post('category_id'), 'category_id');

        echo $category;
    }

And my model:
Code:
function getCategoryInfo($category, $column_name = 'category_id') {
        $query = $this->db->get_where('categories', array($column_name => $category));

        $row = $query->row();

        return $row;
    }
#2

[eluser]CroNiX[/eluser]
Its because your are sending php arrays to the javascript.
In your controller, use something like
echo json_encode(array('result' => $category));

Then in your javascript callback...

function(data){
alert(data.result.category_name); //now you use javascript object
}

Don't forget to set the "type" of your $.post to "json"
#3

[eluser]veritascs[/eluser]
Thanks a bunch Smile




Theme © iAndrew 2016 - Forum software by © MyBB