zachatk1 Posted May 22, 2011 Share Posted May 22, 2011 Hey, I don't know javascript and I have this code that is a dynamic drop down list. The 2 PHP variables are these: $sel1 = $_POST["sel1"] $sel2 = $_POST["sel2"] Here's the javascript, I took out the huge amounts of selections: First_selection = new Array( "1", "2", "3", .... Lots of data ); First selection second array = new Array( ...more data ); function changeval() { var val1 = document.change.sel1.value; var optionArray = eval(val1); for(var df=0; df<optionArray.length; df++) { var ss = document.change.sel2; ss.options.length = 0; for(var ff=0; ff<optionArray.length; ff++) { var val = optionArray[ff]; ss.options[ff] = new Option(val,val); } } } </script> <form name="change" action="verify.php" method="post"> <select name=sel1 onchange=changeval()> <script type="text/javascript"> for(var model=0; model<make.length; model++) { document.write("<option value=\""+make[model]+"\">"+make[model]+"</option>"); } </script> </select> <select name=sel2> </select> When the page loads, the drop down menu needs to have sel1 and sel2 selected in the drop down list. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/237140-populating-a-drop-down-menu/ Share on other sites More sharing options...
sunfighter Posted May 23, 2011 Share Posted May 23, 2011 In the for loop do an if: if (make[model] == <?php echo $sel1 ?>) document.write("<option value=\""+make[model]+"\" selected=\"selected\">"+make[model]+"</option>"); else document.write("<option value=\""+make[model]+"\">"+make[model]+"</option>"); Your going to have to check the \" s but you get the idea Quote Link to comment https://forums.phpfreaks.com/topic/237140-populating-a-drop-down-menu/#findComment-1218908 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.