Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7520111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:00:38+00:00 2026-05-30T02:00:38+00:00

My autocomplete is not working and I can’t spot the error. Jquery : <script>

  • 0

My autocomplete is not working and I can’t spot the error.

Jquery:

<script>
$(function() {
    $('#autoComplete').autocomplete({
        //source: "/groceries/items/autoComplete", ///This works but response isn't formatted correctly'
        //dataType: "json"
        minLength: 2,
        source: function( request, response ) {
            $.ajax({
                url: "/groceries/items/autoComplete",
                dataType: "jsonp",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 12,
                    term: request.term
                },
                success: function( data ) {
                    response( $.map( data, function( el ) {
                        return { label: el.id, value: el.name }
                    }));
                }
            });
        }
    });
});

Controller:

public function autoComplete() {
        Configure::write('debug', 0);
        $this->layout = 'ajax';
        $query = $_GET['term'];
        $items = $this->Item->find('all', array(
            'conditions' => array('Item.name LIKE' => $query . '%'),
            'fields' => array('name', 'id', 'category_id'),
            'group' => array('name')));
        $this->set('items', $items);
    }

Form:

    <p>
    <?php echo $this->Form->create('Item', array('model'=>'item','action' => 'addItem', 'name'=>'AddItem'));?>
        <?php echo $this->Form->text('Item.name', array('size'=>'30', 'id'=>'autoComplete', 'autocomplete'=>'off')); ?>
        <?php echo $this->Form->text('Item.category_id', array('type'=>'hidden', 'value'=>'0')); ?>
        <?php echo $this->Form->text('Groclist.id', array('type'=>'hidden', 'value'=>$groclist['Groclist']['id'])); ?>
    <?php echo $this->Form->end('Add'); ?>
</p>

Response:

0: {Item:{name:Cake, id:6, category_id:null}}
1: {Item:{name:Carrot Cake, id:9, category_id:null}}
2: {Item:{name:Carrots, id:8, category_id:null}}
3: {Item:{name:Casserole, id:11, category_id:null}}
4: {Item:{name:Cauliflower, id:10, category_id:null}}

Edited for clarification.

I realize JqueryUI expects label and value and that map should rearrange them, but for some reason it’s not. Any ideas?


I found an even better solution. This is done completely in the controller. No view required.

 public function autoComplete() {
        Configure::write('debug', 0);
        *$this->autoRender=false;*
        $this->layout = 'ajax';
        $query = $_GET['term'];
        $items = $this->Item->find('all', array(
            'conditions' => array('Item.name LIKE' => $query . '%'),
            'fields' => array('name', 'id', 'category_id'),
            'group' => array('name')));
        *$i=0;
        foreach($items as $item){
            $response[$i]['value']="'".$item['Item']['id']."'";
            $response[$i]['label']="'".$item['Item']['name']."'";
            $i++;
        }
        echo json_encode($response);*
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:00:39+00:00Added an answer on May 30, 2026 at 2:00 am

    What if you reduce the array that is being returned by the Items model. So instead of $this->set('items', $items); you return the json encoded results like so:

    foreach($items as $item) {
        $data[] = $item['Item'];
    }
    $data = json_encode($data);
    echo $data;
    exit;
    

    This is inside the auto_complete method in the controller.

    UPDATE:
    When querying for Cake for example, it would return a result like so:

    [
      {"name":"Cake Batter","id":"1","category_id":"3"},
      {"name":"Cake Mix","id":"2","category_id":"3"}
    ]
    

    if you are not wanting to return the json, you could just return $data without the json encoding.

    Format Update:

    I am not certain if this is to “sloppy”, but you could change the foreach loop to:

    foreach($items as $item) {
        $data[]= array(
            'label' => $item['Item']['name'],
            'value' => $item['Item']['id']
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on this jQuery Autocomplete thing and I can't get the item selected
I have the jquery autocomplete example working on a test page, but can't seem
I am using a jQuery autocomplete script but the problem is I do not
i am using the jquery auto complete script like this $(#ac1).autocomplete('search.php'); And it is
Eclipse autocomplete is not working and is always telling me merely No Default Proposals.
I'm trying to get an autocomplete form working, and I can't seem to figure
I'm using Jorn Zaefferer's Autocomplete query plugin, http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ Everything seems to be working as
I am using a JQuery autocomplete AJAX plugin as follows; $(document).ready(function() { $(#myfield).autocomplete({ serviceUrl:'autocomplete.asp?soc='
Ok guys working on search suggestions using jQuery-UI AutoComplete with results from sql-sever 2008
I am using jQuery autocomplete. Here is my HTML <input class=autocomplete_input> JS $(.autocomplete_input).autocomplete({ source:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.