Derby Artists Posted October 26, 2009 Share Posted October 26, 2009 Hi I have spent half the day searching for some script that creates dynamic lsit/dropdown boxes. Basically depending on the selection from dropdown/list 1 will populate the list of dropdownlist 2. Looking at the code I believe I am going to struggle updating the database, but just need confirmation on this. Here is the html code <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000" onload="fillCategory();"> <FORM name="drop_list" action="yourpage.php" method="POST" > <SELECT NAME="Category" class="qsdropdowns" onChange="SelectSubCat();" > <Option value="">Category</option> </SELECT> <SELECT NAME="SubCat" class="qsdropdowns" id="SubCat"> <Option value="">SubCat</option> </SELECT> </form> </body> And here is the java code function fillCategory(){ // this function is used to fill the category list on load addOption(document.drop_list.Category, "Fruits", "Fruits", ""); addOption(document.drop_list.Category, "Games", "Games", ""); addOption(document.drop_list.Category, "Scripts", "Scripts", ""); } function SelectSubCat(){ // ON selection of category this function will work removeAllOptions(document.drop_list.SubCat); addOption(document.drop_list.SubCat, "", "SubCat", ""); if(document.drop_list.Category.value == 'Fruits'){ addOption(document.drop_list.SubCat,"Mango", "Mango"); addOption(document.drop_list.SubCat,"Banana", "Banana"); addOption(document.drop_list.SubCat,"Orange", "Orange"); } if(document.drop_list.Category.value == 'Games'){ addOption(document.drop_list.SubCat,"Cricket", "Cricket"); addOption(document.drop_list.SubCat,"Football", "Football"); addOption(document.drop_list.SubCat,"Polo", "Polo", ""); } if(document.drop_list.Category.value == 'Scripts'){ addOption(document.drop_list.SubCat,"PHP", "PHP"); addOption(document.drop_list.SubCat,"ASP", "ASP"); addOption(document.drop_list.SubCat,"Perl", "Perl"); } } ////////////////// function removeAllOptions(selectbox) { var i; for(i=selectbox.options.length-1;i>=0;i--) { //selectbox.options.remove(i); selectbox.remove(i); } } So if I choose "Fruits" then "mangos" will the php send the values of "Fruits" and "Mangos" or would it send "Catergory" and "Sub Catergory" to the database. If I am right what other options have I got to populate dropdowns from previous selections Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/179069-will-this-create-problems-when-updating-sql/ Share on other sites More sharing options...
gizmola Posted October 26, 2009 Share Posted October 26, 2009 Just because you are using javascript, doesn't mean that it will change the basic ways that form elements work when a form is posted. Although you didn't include a crucial piece of the javascript code (addOption) I'm assuming it looks like this: function addOption(selectbox,text,value ) { var optn = document.createElement("OPTION"); optn.text = text; optn.value = value; selectbox.options.add(optn); } If so, then the value property should be getting set, and when the form is Posted, the appropriate select should have its value set. You can easily enumerate the values of the $_POST superglob using print_r() to test this and clarify everything to your own satisfaction. Quote Link to comment https://forums.phpfreaks.com/topic/179069-will-this-create-problems-when-updating-sql/#findComment-944796 Share on other sites More sharing options...
Derby Artists Posted October 26, 2009 Author Share Posted October 26, 2009 Thanks gizmola, yes that script is in the java but just didnt copy and paste this, could I also ask would you go down the java route or would you populate the lists through php mysql I always worry about the java being turned off in the users browser. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/179069-will-this-create-problems-when-updating-sql/#findComment-944799 Share on other sites More sharing options...
gizmola Posted October 26, 2009 Share Posted October 26, 2009 You can't get dynamic behavior without javascript or some other clientside technology. The only other option is to require individual requests be sent each time one of the parent select boxes is chosen. That's certainly safe and old school, but in today's dynamic/ajax/web 2.0 world, it may be highly annoying to users who may abandon the form rather than complete it. It's really up to you and the client to decide what you want to invest. It's possible to sniff out the availability of javascript and not use it only if the user doesn't have it available, but then of course that makes the application a lot more complicated. Quote Link to comment https://forums.phpfreaks.com/topic/179069-will-this-create-problems-when-updating-sql/#findComment-944831 Share on other sites More sharing options...
Derby Artists Posted October 26, 2009 Author Share Posted October 26, 2009 Cheers thanks for that. Will give it a bash, I am sure I will be back in a couple of days with post/query issues. but thanks again Quote Link to comment https://forums.phpfreaks.com/topic/179069-will-this-create-problems-when-updating-sql/#findComment-944837 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.