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

  • SEARCH
  • Home
  • 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 9215273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:10:04+00:00 2026-06-18T02:10:04+00:00

I am basically building a common autocomplete search bar where a supposed user writes

  • 0

I am basically building a common autocomplete search bar where a supposed user writes something, jQuery returns a list of suggestions and the user picks one.

My AJAX call looks like this:

var response = '';
var request = $.ajax({
 url: "./includes/search_products.php",
 type: "post",
 dataType: "json",
 data: serializedData,
 success : function(text) {
    response = text; // Gets the list of suggestions
 }
});

The response is:

{"id":"2",
"companyId":"15",
"productTypeId":"1",
"label":"Alfa Romeo 159",
"price":"50000","comments":
"Random comment."}

How I set the .autocomplete:

request.done(function (){
        console.log("Works.");
        $('#product_search').autocomplete({
            source: response,
            minLength: 1,
            select: function(event, ui) {
                alert("yey");
            }
        });
    });

The error message that I get is:

TypeError: this.source is not a function

I believe that a normal response should have less quotation marks, based on what the PHP json_encode() documentation says.

What’s the problem? 🙁

  • 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-06-18T02:10:05+00:00Added an answer on June 18, 2026 at 2:10 am

    Ok, I have found the solution after 2 days. I hope that it will help somebody, someday. No AJAX is worth 8 hours of swearing. This is not a tutorial, but it covers some of the problems that I had in order to get to a working version.

    I was converting an object (say Product) to JSON by using json_encode. The Product class also implements the JsonSerializable interface which consists of one function, jsonSerialize().

    My implementation for jsonSerialize() in the orginal post was longer, but I finally have it sort of like this:

    function jsonSerialize() {
        return [
            'id' => $this->id,
            'label' => $this->name
        ];
    }
    

    For autocomplete to work with an object or actually anything more than a one-dimensional array, you will need to have either a ‘label’ or ‘value’ field. The labels will be displayed as the autocomplete suggestions, but the label will be displayed in the input when you select one of the labels.

    This is how my actual autocomplete function looks:

    $("#product_search").autocomplete({
        source: "./includes/search_products.php",
        minLength: 1,
        select: function(event, ui) {
            /* Do something */
        }
    });
    

    My problem was that I was returning a single object with json_encode($product). What I want to say is that $product = new Product(…). It appears that the source option of the autocomplete function requires an array of Products, not just a single one.

    Solution:

    if(!is_array($product))
        $product = array($product);
    

    My code was not wrong in the original post, just these two lines were missing from my PHP script in order for everything to be fine.

    Over and out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I am building a horizontal navigation bar. I have following markup: <ui:style src=../common.css
We are building a configurator application for conveyors. Basically user comes in and picks
I'm basically building a simple list, and one of the items in the list
I'm building a price estimator form, which uses jQuery to manipulate select menus. Basically,
basically I'm building a custom scroll bar that has a table layout inside it.
I'm building an application which basically has a lot of user input, text boxes,
I'm currently building a bunch of common use controls which basically just extend the
I am building something similar to BBC Zeitgeist . Basically it's a series of
Basically, i'm building a basic jquery modal plugin as practice, and as much as
I'm using IIS7 and ASP.NET. Basically building a simple CMS and need to handle

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.