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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:02:20+00:00 2026-06-04T21:02:20+00:00

I have three HTML DropDownList ( <select><option></option></select> ). The first DropDownList contains categories, the

  • 0

I have three HTML DropDownList (<select><option></option></select>). The first DropDownList contains categories, the second one contains subcategories of different products and the third one contains brands (or manufacturers).

When a category is selected, two drop downs subcategory and brand should be populated at once from the database according to the category id being passed to an Ajax function. I’m using the following Ajax code.

function ajax()
{
    if(window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp = new ActivexObject("Microsoft.XMLHTTP");
    }
}

function getBrandList(selected)  //selected is the category id.
{               
    ajax();
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("brandList").innerHTML=xmlhttp.responseText;            
        }
    }

    xmlhttp.open("GET","ajax/BrandAjax.php?selected="+selected, true);
    xmlhttp.send();     
    alert(selected);            
}

function getSubcategoryList(selected) //selected is the category id.
{                       
    getBrandList(selected); //First above function is invoked to populate brands.
    ajax();
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("subCategoryList").innerHTML=xmlhttp.responseText;                              
        }
    }

    xmlhttp.open("GET","ajax/SubCatAjax.php?selected="+selected, true);
    xmlhttp.send();             
}   

When a category is selected, the getSubcategoryList(selected) Javascript function is invoked which does the Ajax request. The problem is that I need to populated both subcategory and brand drop down at once (when a category is selected).

It is working and both the drop downs are populated at once according to the category id being passed (it’s the parameter of the above functions selected).

I’m unnecessarily using an alert box at the bottom of the function getBrandList(). When this alert box is commented, only one drop down which is subcategory is populated. Brands remain empty. I don’t need this alert box anymore.

Why does this happen? What is the solution?

  • 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-04T21:02:23+00:00Added an answer on June 4, 2026 at 9:02 pm

    seriyPS’s basically hit the target on this. The issue is in fact the xmlhttp variable. You need to declare it local within each function it is being used. Basically, create a closure for the function calls. The issue isn’t necessarily that it needs to be “local” however, but it is being redefined in the second request as a result of being a global variable. This essentially kills the initial request because the variable now points to the second request.

    The reason the alert box makes it work is because the first request is finishing the ajax request before you can “click” ok. (The alert box will pause the javascript execution and therefore delay the second request until after you have clicked ok.)

    To fix this, you can modify your code to use a different variable for each request. Try changing your function to something like this:

    // Global Variables
    var brandListRequest;
    var subcategoryRequest;
    
    function ajax()
    {
        var xmlhttp; //!!
        if(window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp = new ActivexObject("Microsoft.XMLHTTP");
        }
        return xmlhttp; //!!
    }
    
    function getBrandList(selected)  //selected is the category id.
    {               
        brandListRequest = ajax(); //!!
        brandListRequest.onreadystatechange=function()
        {
            if(brandListRequest.readyState==4 && brandListRequest.status==200)
            {
                document.getElementById("brandList").innerHTML=brandListRequest.responseText;            
            }
        }
    
        brandListRequest.open("GET","ajax/BrandAjax.php?selected="+selected, true);
        brandListRequest.send();     
        //alert(selected);            
    }
    
    function getSubcategoryList(selected) //selected is the category id.
    {                       
        getBrandList(selected); //First above function is invoked to populate brands.
        subcategoryRequest = ajax(); //!!
        subcategoryRequest.onreadystatechange=function()
        {
            if(subcategoryRequest.readyState==4 && subcategoryRequest.status==200)
            {
                document.getElementById("subCategoryList").innerHTML=subcategoryRequest.responseText;                              
            }
        }
    
        subcategoryRequest.open("GET","ajax/SubCatAjax.php?selected="+selected, true);
        subcategoryRequest.send();             
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML markup:- <select name=List1 id=l1> <option>One</option> <option>Two</option> <option>Three</option> <option>Four</option> <option>Five</option>
I have three files in a folder 'test' one.php two.php print.html And i have
I have 5 pages - for ease lets say: one.html two.html three.html four.html five.html
I have a single HTML file that contains three div elements in it, Each
I have follwoing code @Html.DropDownList(optionsforuser, new SelectList(new[] { Option1, Option2, Option3 }), Select) Is
I have a table in HTML which has three columns i.e Error - User
Suppose I have an HTML page with three blocks of fixed width (their height
I have written a html page where i want a header,content,footer. All these three
I want to show three XMLs on a single html page. These XMLs have
I have a @Html.DropDownList in the View. I like to default the value of

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.