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

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):
function getCat(obj) {
    var catID = obj[obj.selectedIndex].value;
    $.post("categories/getCat", { category_id: catID },
           formObject = document.forms['modify_category'];
           formObject.elements['category_name'].value = data["category_name"];//THIS IS WHERE IM SUCK

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

        echo $category;

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

        $row = $query->row();

        return $row;

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...

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

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

Thanks a bunch Smile

Theme © iAndrew 2016 - Forum software by © MyBB