ronc0011 Posted September 5, 2017 Share Posted September 5, 2017 I’m working on building a web (html) form for a vehicle maintained app. I think most of it will be pretty straight forward but the first problem is building the dropdown list for Make & Model. I have found some info on doing this with javascript but I’m wondering if there is a way to do this with PHP. Basically I need to make a dropdown list of Automobile brands / makes i.e. Ford, Chevy, Dodge, etc. Next I need a second dropdown list of models. The models need to be dependent on the Make selected in the first, “Make” dropdown. So if Ford is selected on the “Make” dropdown then the options shown in the “Model” dropdown will be appropriately, Mustang, Taurus, Focus, F50, etc. The Javascript I’ve found looks like this… function populate(s1,s2){ s2.innerHTML = ""; if(s1.value == "Chevy"){ var optionArray = ["|","camaro|Camaro","corvette|Corvette","impala|Impala"]; } else if(s1.value == "Dodge"){ var optionArray = ["|","avenger|Avenger","challenger|Challenger","charger|Charger"]; } else if(s1.value == "Ford"){ var optionArray = ["|","mustang|Mustang","shelby|Shelby"]; } for(var option in optionArray){ var pair = optionArray[option].split("|"); var newOption = document.createElement("option"); newOption.value = pair[0]; newOption.innerHTML = pair[1]; s2.options.add(newOption); } } </head> <body onload="process()"> <h2>Choose Your Car</h2> <hr /> Choose Car Make: <select id="slct1" name="slct1" onchange="populate(this.id,'slct2')"> <option value=""></option> <option value="Chevy">Chevy</option> <option value="Dodge">Dodge</option> <option value="Ford">Ford</option> </select> <hr /> Choose Car Model: <select id="slct2" name="slct2"></select> <hr /> </body> </html> This code does in fact work though I’m not actually seeing how to capture and use the selected values from the second list. Actually that’s why I’m looking to see if there is a PHP approach to this. This code kind of loses me at the end where its creating newOptions and where it’s storing the selected values. Quote Link to comment https://forums.phpfreaks.com/topic/304870-how-to-program-html-form/ Share on other sites More sharing options...
requinix Posted September 5, 2017 Share Posted September 5, 2017 So you know, this approach is fine for small list of models, but if you need to expand it much further then you need something better. The Javascript will fill in the with s that have the appropriate value and display label. If you choose Chevy then the HTML for the model select will effectively be <option value=""></option> <option value="camaro">Camaro</option> <option value="corvette">Corvette</option> <option value="impala">Impala</option>You can get the value using $_GET["slct2"] like it was a normal form field. Quote Link to comment https://forums.phpfreaks.com/topic/304870-how-to-program-html-form/#findComment-1550762 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.