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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:53:33+00:00 2026-06-05T00:53:33+00:00

I have two jQuery AJAX request being made when the use searches for customer

  • 0

I have two jQuery AJAX request being made when the use searches for customer information.
I can get the customer name via .autocomplete and I can get JSON data from addressSearch.php, but when I cannot get the values i need from the second JSON set.

The user should simply select the address from the drop down and i should fill in the form.

When I console.log(address) I do not get the correct multidimensional array.

How do I select the correct sub-array to send data to the form fields?

HTML

<label>Customer Search:</label><input id="cSearch" type="text" /><br />
<label>Name:</label><input id="name" /><br />
<select id="addresses">
  <option>Please Select Address</option>
</select>
<label>Street:</label><input id="street" type="text" /><br />
<label>City:</label><input id="city" type="text" /><br />
<label>State:</label><input id="state" type="text" /><br />
<label>Zip:</label><input id="zip" type="text" /><br />

JavaScript

$(document).ready(function() {
    $("input#customerSearch").autocomplete({
        source:'inc/customerSearch.php',
        select: function(event, ui) {
                $('#name').val(ui.item.name);
                getAddresses(ui.item.addressid);
            },
            minLength:2
        });
    });
    var address = [];
    function getAddresses(id){
        $.getJSON(
            'inc/addressSearch.php',
            { id: id },
            function(data)  {
                $('#addresses').children().remove().end().append('<option>Please Select Address</option>');
                $.each(data, function(x, val) {
                    $('#addresses').append('<option value="' + val['addressid'] + '">'+ val['street1'] +'</option>');
                    address[x] = [];
                    $.each(val, function(key, value) {
                        address[x][key] = value;
                    });
                });
            }
        );
    $('select#addresses').change(function() {
        var j = $(this).val();
        //fill in fields when select 
    });
}

addressSearch.php

$id = explode(',', $id);//explode address ids
$data = array();//data array
foreach($id as $id) {//evaluate each address
    $query = "SELECT * FROM `address` WHERE address_id = '". $id ."'";//query
    $result = mysql_query($query);//run query
    if (mysql_num_rows($result)) {//results exist
        while($row = mysql_fetch_assoc($result)) {//loop
            $data[$id] = array(//build array
                'addressid' => $row['address_id'],
                'street1' => $row['address_street1'],
                'street2' => $row['address_street2'],
                'city' => $row['address_city'],
                'region' => $row['address_region'],
                'country' => $row['address_country'],
                'code' => $row['address_code']
            );
        }
    }
}
print json_encode($data);// jQuery wants JSON data

JSON

{
    "101":{
        "addressid":"101",
        "street1":"123 ABC St",
        "street2":"",
        "city":"Somewhere",
        "region":"AB",
        "country":"USA",
        "code":"12345"
    },
    "102":{
        "addressid":"102",
        "street1":"123 10th St",
        "street2":"Apt 101",
        "city":"Nowhere",
        "region":"AB",
        "country":"USA",
        "code":"12345"
    }
}
  • 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-05T00:53:34+00:00Added an answer on June 5, 2026 at 12:53 am

    I just had to walk away and look at it again. There was nothing wrong with the code above, it was the second parts that wasn’t working.

    Here is the final function

    JavaScript

    $('select#addresses').change(function() {
            var j = $(this).val();
            $('#street1').val(address[j]['street1']);
        $('#street2').val(address[j]['street2']);
        $('#city').val(address[j]['city']);
        $('#code').val(address[j]['code']);
    
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jQuery ajax request in a .js file: $.ajax({ type: GET,
I have two web pages; Foo.aspx and Bar.aspx. Bar.aspx uses jQuery.ajax() to request a
I have two jQuery UI custom builds; they were made by different people from
I have two lists in which using jQuery sortable i can move the items
I'm new to jquery and ajax - just can't seem to get this to
I have an jquery ajax request that calls a PHP script that will send
I have two jQuery Ajax calls that I'm combining on a page. I'm stuck
Can jquery-ajax reload a div WITH DYNAMIC CONTENTS. For example if i have a
I am working on a filter functionality using ajax/jquery and php/mysql.I have two sets
I have two Jquery functions one to show a zoom / detail of the

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.