Jump to content

How to Program html form


ronc0011

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
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.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.