namikmik Posted December 16, 2007 Share Posted December 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 16, 2007 Share Posted December 16, 2007 http://www.google.com/search?q=Dynamic+Chained+Select+Menu&btnG=Search&hl=en Quote Link to comment Share on other sites More sharing options...
namikmik Posted December 16, 2007 Author Share Posted December 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 16, 2007 Share Posted December 16, 2007 if you need it to automatically update when a new value is entered into your database (without having to reload the page) - your going to have to find you a AJAX routine; which is JavaScript interacting with Server Side Language. Quote Link to comment Share on other sites More sharing options...
namikmik Posted December 16, 2007 Author Share Posted December 16, 2007 Yes something like that will be really cool. thnx... Quote Link to comment Share on other sites More sharing options...
AntiScourge Posted December 16, 2007 Share Posted December 16, 2007 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.. Quote Link to comment Share on other sites More sharing options...
namikmik Posted December 16, 2007 Author Share Posted December 16, 2007 AntiScourge, thnx you saved a lot of time. This was what I was looking for, thnx to the all the others for reading my request and thnx to phpfreaks forum. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.