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 != ""){
}
});
});
</script>
Just note that the SELECT * statement in the model will return tree_id, tree_name and tree_description.