Welcome Guest, Not a member yet? Register   Sign In
load data using jquery .change event from a method
#3

[eluser]TheFuzzy0ne[/eluser]
Sorry, I misread your question.

If you can tell us what you want your controller to return, we might be able to help you. At the moment, it's hard to tell, since any response from the server will replace your entire page, removing anything that already existed between the <body ...> tags. For someone who has done this a thousand times, I find that disconcerting. Wink

I suspect you want to return the HTML code to insert into your dropdown menu?

I've made a few assumptions here, since I don't know your table structure, field names, or possible values.

Your model method:
Code:
public function getRolesByDepartment($deptid, $html = FALSE)
{
    // We only need two fields for this, I've assumed they're called 'id' and 'name'.
    $query = $this->db
        ->select('id, name')
        ->where('dept_id', $deptid)
        ->get('school_user_role')->result();
    
    if ( ! $query)
    {
        return FALSE;
    }
    
    if ( ! $html)
    {
        return $query;
    }
    
    $ret = '<option value="0">Please select...</option>';
    foreach ($query as $row)
    {
        $ret .= '<option value="' . $row-&gt;id . '">' . htmlentities($row->name) . '</option>';
    }
    
    return $ret;
}

It's up to make your controller respond correctly to the request.

Also, a couple of notes:

The syntax you're using for $this->db->where(), whilst (kinda) correct, can be achieved much more easily (see my code above).
If you're selecting all fields, you don't need to use $this->db->select('*'). If you don't specify any fields to select, '*' is the default value.
You don't need to use $this->db->from(). You can pass the table name to $this->db->get(). This is more a personal preference, because it saves a little bit of code. An argument could be made that your method is more readable, so I leave it to you to decide.
The method I posted could be made a lot better. I'm not entirely sure on how you might need the data returned, so I've left it as-is, unless you pass TRUE in as the second parameter, in which case, you'll get the options returned as HTML. I think I'd break that up into two methods. The first would return an associative array which could be used with form_dropdown(), and the other would take the returned data and format it into HTML for your AJAX request.


Messages In This Thread
load data using jquery .change event from a method - by El Forum - 04-29-2013, 07:48 AM
load data using jquery .change event from a method - by El Forum - 04-29-2013, 10:07 AM
load data using jquery .change event from a method - by El Forum - 04-29-2013, 10:26 AM
load data using jquery .change event from a method - by El Forum - 04-29-2013, 10:58 AM
load data using jquery .change event from a method - by El Forum - 04-29-2013, 11:07 AM
load data using jquery .change event from a method - by El Forum - 04-29-2013, 11:11 AM
load data using jquery .change event from a method - by El Forum - 04-29-2013, 12:09 PM



Theme © iAndrew 2016 - Forum software by © MyBB