KingOfHeart Posted February 14, 2011 Share Posted February 14, 2011 In my first dropdown box I want to have these two(maybe more later) words. OZ Functions Scripting Basics Now if the first dropdown box selects OZ Functions then I need it to use this array for the second dropdown box $t = array("???","general","entity","animation","counter","core","float","enhanced"); If the first dropdown box selects Scripting Basics I then need it to show this array for the second dropdown box $t = array("???","Basic Logical Operators","Basic Relational Operators","Basic keywords","Integer numeric constants","Predefined constants","Predefined tag names","Character constants"); The size of the array may change, but can you help get me started by telling it what to show in the second drop down box? I plan on using the values 0 - 7 for the first array and 20 - 27 for the second. I'm using a big gap incase if I add more commands so I don't have to edit the mysql table fields. Quote Link to comment https://forums.phpfreaks.com/topic/227581-change-second-dropdown/ Share on other sites More sharing options...
jcanker Posted February 18, 2011 Share Posted February 18, 2011 try something like this: use a different variable name for each of the arrays for the second select list (arrayA and arrayB?) and build each array (then you can change/add to each later as necessary without changing the rest of the code) Get the value of select1 start a new string variable and use it to start the 2nd select, such as: var select2 = "<select id='select2'>"; run if/else statement like if select1.value is OZ In the if/else: [*]step through arrayA and append each item as an <option> to select2 [*]in the else, step through arrayB and append each item as an option to select2 After the if/else close out select2: select2 +="</select>" attach select2 to the dom in the appropriate location/div In your document load/ready section: attach the onchange event handler to call the new function. Doing it this way will essentially create the 2nd option list on the fly when something is selected off the first list. If you want the list to appear already, just include it in the html and remove the steps where you start/end select2 with <select>and </select>; just build the <option>'s and add them in. This will require a little bit more finnesse with your element selectors to get it in the right spot (or you can include <select>&</select> and just replace the ones in the existing html). jQuery would make this easier, btw, because it's selector is so powerful. Quote Link to comment https://forums.phpfreaks.com/topic/227581-change-second-dropdown/#findComment-1176007 Share on other sites More sharing options...
jcanker Posted February 18, 2011 Share Posted February 18, 2011 And you'll want to add something to the onclick handler to flush the select2 list before it builds it in the function--that way if they change their selection on select1 the code will still work. Quote Link to comment https://forums.phpfreaks.com/topic/227581-change-second-dropdown/#findComment-1176010 Share on other sites More sharing options...
KingOfHeart Posted February 18, 2011 Author Share Posted February 18, 2011 Uh, I don't have too much experience with java. So not sure what to do exactly. Quote Link to comment https://forums.phpfreaks.com/topic/227581-change-second-dropdown/#findComment-1176018 Share on other sites More sharing options...
jcanker Posted February 18, 2011 Share Posted February 18, 2011 lol...I think you mean javascript, not java. 2 entirely different animals if you're trying to learn javascript to do this project, I suggest you learn jQuery first--it's a javascript framework that DRAMATICALLY eases the learning curve for javascript and eliminates most of the cross-browser issues that you have to work around in javascript. What can take you many lines in javascript you can do in just a few lines using jQuery. No one around here is going to write your code for you, but we'll write short snippets or offer an example. If we write the code for you, you won't learn. I've written everything for you in psuedocode, now you just need to learn jQuery and put it together. I HIGHLY recommend the book jQuery: Novice to Ninja. I've been through a bunch of books recently to learn javascript/jQuery, and this one is by far the best one. Rather than just throw some random projects at you, it walks you through building an actual jQuery project -a robust website--with a consistent context from beginning to end. Quote Link to comment https://forums.phpfreaks.com/topic/227581-change-second-dropdown/#findComment-1176026 Share on other sites More sharing options...
KingOfHeart Posted February 18, 2011 Author Share Posted February 18, 2011 Sorry I don't read books. I get bored too easily. So far I learned html, php, mysql, just a dash of c++. How did I learn this... by copying and pasting different scripts and then memorizing them. So if I get different versions of this script, I got a higher chance of learning it. Quote Link to comment https://forums.phpfreaks.com/topic/227581-change-second-dropdown/#findComment-1176362 Share on other sites More sharing options...
KingOfHeart Posted February 20, 2011 Author Share Posted February 20, 2011 I found what I needed online but it's not 100% correct. <script> function loadTypes() { var ozfun=new Array("???","general","entity","animation","counter","core","float","enhanced"); var ozbac=new Array("???","Basic Logical Operators","Basic Relational Operators","Basic keywords","Integer numeric constants","Predefined constants","Predefined tag names","Character constants","Misc","Advanced"); var selectedBoxValue=document.functions.inc1.value; var i; var j; var inc2Length=eval(selectedBoxValue).length; removeSelectboxOption(); for(i=0;i<document.functions.inc1.options.length;i++) { if(selectedBoxValue==document.functions.inc1.options[i].value) { for(j=0;j<inc2Length;j++) { document.functions.inc2.options[j]=new Option(eval(selectedBoxValue)[j],eval(selectedBoxValue)[j]) } } } } function removeSelectboxOption() { var i; for(i=0;i<document.functions.inc2.options.length;i++) { document.functions.inc2.remove(i); } } </script> I just need the value of the box to be 0 - 7 for the second box if you choose ozfun for the first box. Or 10 - 19 for the second box if you choose ozbac for the first box. Quote Link to comment https://forums.phpfreaks.com/topic/227581-change-second-dropdown/#findComment-1177008 Share on other sites More sharing options...
KingOfHeart Posted February 20, 2011 Author Share Posted February 20, 2011 Not sure if I caused confusion or not. At this point the select value are general, entity, animation.... and basic logical operators, basic relational operators, basic keywords,... I don't want strings for these values, I want numbers. Quote Link to comment https://forums.phpfreaks.com/topic/227581-change-second-dropdown/#findComment-1177090 Share on other sites More sharing options...
KingOfHeart Posted February 21, 2011 Author Share Posted February 21, 2011 Even though I got it working, I still need it to be able to have a default selected based off of some mysql data. At this point I say screw it. I'll just make one box. Quote Link to comment https://forums.phpfreaks.com/topic/227581-change-second-dropdown/#findComment-1177705 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.