Jump to content

[SOLVED] Dynamically fill two listboxes


namikmik

Recommended Posts

Hi to all,

 

Sorry if I am double posting!!!!!!!!

 

I am working on a project in my Uni, now I come to a situation that I have to fill a listbox from the selected value of an other listbox that is in the same form.

 

To be more specific:

"The first listbox is filled with a simple query in mysql, the second listbox is still filled form a mysql query but this query is depended from the selected value of the first listbox."

 

How can I do this?!!!

 

Any kind of help will useful.

 

With Respect,      Namik Shehu

Link to comment
Share on other sites

Thnx a lot phpQuestioner, really appreciated.

But I still I couldnt solve my problem,

my listboxes need to be populated dynamically, so if an other record is added to my db it must reflect in the listbox automatically, without needing me to inter vent manually to the js code plus I am new in js.

 

Thnx again for your time, still waiting for an other solution

 

Link to comment
Share on other sites

Lucky You! I just created it yesterday.

On my form I have a Category List Box, and a Sub-Category List Box

 

Category table has two fields, CategoryID and Title

SubCategory table has three fields. SubCategoryID, CategoryID(relates to a category in the category table), and Title

The Category list is populated with the following code

<select class="new" name="seCategory" id="seCategory" onchange="UpdateSubCat()">
  <?
    $query = "SELECT * FROM categories ORDER BY title";
    $results = mysql_query($query);
    while($row = mysql_fetch_array($results)) {
      echo "<option value='" . $row['CategoryID'] . "'>" . $row['Title'] . "</option>";
    }
?>
</select>

 

The following JavaScript code is called when the page first loads, and whenever someone changes the selection in the list.

      function UpdateSubCat() {
        var catID = document.getElementById('seCategory').value;
        var subCatMenu = document.getElementById('seSubCategory');
        subCatMenu.options.length = 0;

        var ajaxRequest;  // The variable that makes Ajax possible!
        try{
          // Opera 8.0+, Firefox, Safari
          ajaxRequest = new XMLHttpRequest();
        } catch (e){
          // Internet Explorer Browsers
          try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (e) {
            try{
              ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
              // Something went wrong
                alert("Your Internet Browser is not compatible!");
                return false;
            }
          }
        }
        ajaxRequest.onreadystatechange = function(){
          if(ajaxRequest.readyState == 4){
            var subCats = ajaxRequest.responseText;
            var scVal;
            var scTitle;
            while (subCats != "") {
              var scOption = document.createElement('option');
              scVal = subCats.substr(0, subCats.indexOf(","));
              scTitle = subCats.substr(scVal.length + 1, subCats.indexOf(";") - scVal.length - 1);
              subCats = subCats.substr(scVal.length + scTitle.length + 2);
              scOption.value = scVal;
              scOption.text = scTitle;
              try {
                subCatMenu.add(scOption, null);
              } catch(e) {
                subCatMenu.add(scOption);  //IE-Compliant
              }
            }
          }
        }
        var queryString = "?q=" + catID;
        ajaxRequest.open("GET", "pages/scripts/subcat-sql.php" + queryString, true);
        ajaxRequest.send(null); 
      }

 

The subcat-sql.php file is the script that queries the mySQL database for a list of subcategories related to the current category ID.  It returns them as CategoryId,Title;CategoryID,Title;Cat..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.