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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:48:23+00:00 2026-06-11T08:48:23+00:00

Okay so I’m using the jQuery autocomplete working with my database of locations and

  • 0

Okay so I’m using the jQuery autocomplete working with my database of locations and states in the US, the goal is the user starts typing the city, town, etc and I display City (location in my DB), State (region in my DB) Zip (postalCode in my DB) to them and when they select one it fills the city state and zip fields.. My problem is that when I choose an option, the state and zip code fill in correctly, however the city field takes the value of the zip code and I can’t figure it out.. Here is my JS code followed by the separate PHP file that pulls in my DB data.. I’m more experienced in PHP than JS but you never know where you’ll make a mistake…

The JS/jQuery File:

$(document).ready(function(){
var ac_config = {
    source: "autocomplete-l.php",
    select: function(event, ui){
        $("#scity").val(ui.item.location);
                    $("#sstate").val(ui.item.region);
        $("#szip").val(ui.item.postalCode);
    },
    minLength:1
};
$("#scity").autocomplete(ac_config);
});

And the PHP File:

$cities = array();
$sth = $dbh->prepare("SELECT * FROM locations WHERE country = 'US'");
$sth->execute();

while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$cities[]=$row;
}

$term = trim(strip_tags($_GET['term']));

$matches = array();
foreach($cities as $city){
if((stripos($city['location'], $term) !== false)){
    // Add the necessary "value" and "label" fields and append to result set
    $city['value'] = $city['location'];
    $city['value'] = $city['region'];
    $city['value'] = $city['postalCode'];
    $city['label'] = "{$city['location']}, {$city['region']}{$city['postalCode']}";
    $matches[] = $city;
    }
}

$matches = array_slice($matches, 0, 100);
print json_encode($matches);
  • 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-11T08:48:24+00:00Added an answer on June 11, 2026 at 8:48 am

    call event.preventDefault(); inside the select-function.

    Otherwise the automplete will proceed with the default-action:

    The value will be inserted into the input element
    after the user selected something from the menu

    The input-element is $("#scity")

    $("#scity").autocomplete(ac_config);
    

    and the value is the postal-code:

    $city['value'] = $city['postalCode'];
    

    Note these lines:

    $city['value'] = $city['location'];
    $city['value'] = $city['region'];
    $city['value'] = $city['postalCode'];
    

    …you overwrite $city['value']

    you can use only this:

    $city['value'] = $city['location'];
    

    and remove that line from the select-function:

    $("#scity").val(ui.item.location);
    

    (because $.autocomplete will set the value of $("#scity") automatically)

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

Sidebar

Related Questions

Okay, so I'm working on a java server for an Apps backend, it must
okay, i'm setting up a multi-user chat system. i have a messages table, that
Okay, here's the rundown. I'm developing a video hosting site with PHP and jQuery
Okay, basically I've developed a simple image upload system. The user selects a local
Okay say your using a app, and you opened a new activity and then
Okay, I am using a custom tab bar (not a tab based app) but
Okay so here is my database: I have a page on my .net form
Okay, so my paddle collision is working fine: if(velo.y > 0){ float t =
Okay so im working on this php image upload system but for some reason
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My

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.