Code Igniter php - return (ajax?) Data based on dropdown using jQuery

I want to generate some data for editing, filtered by the userโ€™s choice in the drop-down menu, but I donโ€™t want to complicate the situation.

I already have a project that queries the database for a list of "trees" and populates the dropbox with the tree name, assigning the value tree_id as its value.

All I want to do is when the user makes a selection in the drop-down list, jQuery returns a list of only that identification data (description, etc.).

What is the easiest way to do this? Do I even need to make an AJAX request? Here is my code:

CONTROLLER:

$this->load->model('Model_form','', TRUE);
$data['trees'] = $this->Model_form->get_tree();
$this->load->view('view_form_tree', $data);

Model:

function get_tree(){
$query = $this->db->query('SELECT * FROM trees');
return $query->result();
}

VIEW:

<h1>Edit Tree</h1>
<select id="f_treeindex" name="f_treeindex" class="dd_black">
<option value=""></option>
<?php
foreach($trees as $tree){
    echo '<option value="' . $tree->id . '">' . $tree->tree_name . '</option>';
}
?>
</select>

<script type="text/javascript">
     $(document).ready(function() {
        $('#f_treeindex').change(function(){
            var tree_id = $('#f_treeindex').val();
            if (tree_id != ""){
                //DO WHATEVER I NEED TO DO TO CALL THE INFORMATION FOR THE TREE WHOSE ID MATCHES THAT SELECTED IN THE DROPDOWN
            }//end if
        }); //end change
     }); //end docready
</script>

Just note that the SELECT * statement in the model will return tree_id, tree_name and tree_description.

+3
1

show, (.. ), - :

$('#f_treeindex').change(function(){
    var tree_id = $('#f_treeindex').val();
    if (tree_id != ""){
        $.get('/controller_name/show', {id:tree_id}, function(data){
            $(this).parents('div').append(data);
        })
    }//end if
});

<select> <div>, jquery -, html .

, get_tree() get_trees(), get_tree ($ id), controller_name # show, , .

function show() {
    $this->load->model('Model_form','', TRUE);
    $data['tree'] = $this->Model_form->get_tree($this->params['id']);
    $this->load->view('single_tree_view', $data);
}

100% , CI , $.get

$.get('/controller_name/show/'+tree_id, function(data){...
+4

All Articles