Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/304870-how-to-program-html-form/
Share on other sites

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

<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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.