Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter, JQuery, Autocomplete - can't get database list to show on page
#1

[eluser]mlynno[/eluser]
I've combed through the forums hoping to find an answer for this, but no luck. I'm not able to get the autocomplete choices to show up in my view file; I've checked my model and I know that it is pulling the appropriate records from the database, but somewhere there's a disconnect (probably something stupid). I'm using CI 2.0. Any help would be greatly appreciated.

Here's my controller:
Code:
class Autocomplete extends CI_Controller {
    function admin_autocomplete()
{
        $this->load->model('Autocomplete_model');
        $keyword = $this->input->post('searchterms');
        $data['response'] = 'false';
        $query = $this->Autocomplete_model->searchterms($keyword);
        if($query->num_rows() > 0){
           $data['response'] = 'true';
           $data['message'] = array();
           foreach($query->result() as $row){
           $data['message'][] = array('label'=> $row->genus, 'value'=> $row->genus);
           }
        }
        echo json_encode($data);
}}
Model:
Code:
class Autocomplete_model extends CI_Model
{
    public function searchterms($keyword)
    {
        $this->db->select('id, genus');
        $this->db->from('plant_data');      
        $this->db->like('genus', $keyword);
        $this->db->order_by("genus", "asc");

        $query = $this->db->get();
        foreach($query->result_array() as $row){
           $data[] = $row;
        }
        return $query;
    }
}
And view (I've left the javascript in the view for now):
Code:
[removed]
        $(function() {
        $("#searchterm").autocomplete({
    minLength: 2,
    source: function(req, add){
        $.ajax({
            url: '<?php echo base_url(); ?>autocomplete/admin_autocomplete',
            dataType: 'json',
            type: 'POST',
            data: req,
            success: function(data){
                if(data.response =='true'){
                   add(data.message);
                }
            }
        });
    }
});
        }
        [removed]
.... (search form)
<?php
    $attributes = array('id' => 'searchform');
    echo form_open('listplants/search', $attributes); ?>
    <input type="text" name="searchterms" id="searchterms">
    <input type="submit" value="Search">
?>
.....
Thanks for taking a look!
#2

[eluser]toopay[/eluser]
Try add proper header (Content-Type) before echoing/outputing the json.
#3

[eluser]mlynno[/eluser]
Hi and thanks for your quick reply. This is my current header:
Code:
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
I'm not really familiar with JSON so please let me know if I need to change that.
#4

[eluser]toopay[/eluser]
I mean the HTTP header while you outputing the json. Like
Code:
// ... in your controller
// Instead this...
// echo json_encode($data);
// Do these...
$this->output
     ->set_content_type('application/json')
     ->set_output(json_encode($data));
#5

[eluser]mlynno[/eluser]
Thank you -- I just gave that a try, but no luck.
#6

[eluser]toopay[/eluser]
Erm... Looks like you will need to debug your code to see whether the ajax is bringing back the json or not (if you using firebug, you can use 'console.log(somevariabletolog)' too see the json value)
#7

[eluser]mlynno[/eluser]
Thanks -- Firebug did discover a missing ) at the end of the javascript in the view, didn't solve the problem though. Any other suggestions would be appreciated. Thanks again!
#8

[eluser]Rok Biderman[/eluser]
Eh, you have some debugging to do - may I suggest 2 points where I don't see the logic.
Code:
$("#searchterm").autocomplete({
everywhere else you use id searchterms
Code:
source: function(req, add)
I don't see req defined anywhere, maybe we're not seeing something. Some of your script code got slashed as you didn't use lt/gt syntax with script tags, when you posted and I might be missing something else as well.

Basically, the first thing you need to see is if you're sending post data you want and getting the response you need (both in firebug net tab).
#9

[eluser]mlynno[/eluser]
Thank you -- and I am so embarrassed. "searchterm" was the key; I changed it to "searchterms" and it works. It's always those little things... Thank you again for your assistance.
#10

[eluser]Jan_1[/eluser]
Trying the same, but it doesn't work. I want an input field with autocomplete of groupnames from my database.
Sorry for the short question, but, Do you see a mistake?

View:
Code:
$(function() {
        $("#groups").autocomplete({
    minLength: 0,
    source: function(req, add){
                $.ajax({
            url: '<?php echo base_url(); ?>index.php/controller/ajax_groups',
            dataType: 'json',
            type: 'POST',
            data: req,
            success: function(data){
                if(data.response =='true'){
                   add(data.message);
                }
            }
        });
    }
});
        })
<!-- script-tags, of course -->
<input name="group" id="groups"/>
Controller:
Code:
function ajax_groups()
    {
       $keyword = $this->input->post('term');
       $data['response'] = 'false';
       $query = $this->form_model->searchterms($keyword);
       if($query->num_rows() > 0)
            {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach($query->result() as $row){
            $data['message'][] = array('label'=> $row->group, 'value'=> $row->group);
           }
        }
        $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($data));
    }

Model:
Code:
public function searchterms($keyword)
    {
        $this->db->select('group');
        $this->db->from('my_groups_in_database');
        $this->db->like('group', $keyword);
        $this->db->order_by("group", "asc");
        $this->db->group_by('group');
        $query = $this->db->get();
        foreach($query->result_array() as $row){
            $data[] = $row;
        }
        return $query;
    }




Theme © iAndrew 2016 - Forum software by © MyBB