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 4554584
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:06:23+00:00 2026-05-21T17:06:23+00:00

I’ve got autocomplete working fine, but I’m trying to figure out way to make

  • 0

I’ve got autocomplete working fine, but I’m trying to figure out way to make it faster. Here’s my HTML:

Country: <select name='country' id='country' onchange="renewAutocomplete();">
             <option value='US'>USA</option>
             <option value='UK'>United Kingdom</option>
         </select><br/>
City: <input type='text' name='city' id='city' />
<script>
   function renewAutocomplete() {
      $( "#city" ).autocomplete({
            source: "searchCities/" + $('#country').val(),
            minLength: 2
        });
   }
</script>

The PHP/mySQL query is pretty basic:

$term = $_GET['term'];
$sql = "SELECT city FROM cities WHERE country_code = '{$country}' AND city LIKE '{$term}%'";

OF course, this means a new query for every keystroke. It helps a bit to add some caching, but it still isn’t as fast as I think it should be.

Question 1
My first thought was to get ALL the cities in one db call when the Country is selected. Is there a way to assign the returned JSON of all the cities of country into a variable that autocomplete can use? My initial tests would just popup all the cities as you typed:

      $.get("searchCities/" + $('#country').val(), function(data) {
                    $( "#city" ).autocomplete({
                        source: data,
                        minLength: 2
                    });
        });
PHP: 
    $sql = "SELECT city FROM cities WHERE country_code = '{$country}'";

Question 2
Since cities and countries don’t change a lot (usually), would it be faster to just have a flat file with all the values?

Or is there any other way to make a faster autocomplete?

Thanks.

  • 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-21T17:06:24+00:00Added an answer on May 21, 2026 at 5:06 pm
    1. Autocomplete assumes that if you return it, it’s a valid answer. So: no.

    2. Yes, pulling from a flat file would be a lot faster than hitting your db for every key stroke. However, if you threw some caching (memcache) in front of the db, it would probably be just as fast as a flat file.

    But really, because you’re (likely) searching a (relatively) small set for your term, it’s going to be really fast anyway.

    From a scaling perspective, stick them in a flat file or use caching.

    As a side note: look into mysql_escape_string() for using $country and $term. Otherwise you’re open to SQL Injection attacks.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.