Hi all, hope I can get some help.
I am very very new to PHP so bare with me.
I am doing a system that has information about cars.
My drop downs are populated with MySQL data (working ok)
I want to have one drop down for Make of car, then then enables another for the model.
I have been doing a ton of research and have gotten some great ideas how to link my database and select data from it, etc. A lot from this site, I am learning a lot.
I need the selected item from the first drop down to be saved in a string variable essentially, whether that is the text or label for that item, I really don't care.
For example, if they select "ford" from the drop down, I need the word "ford" saved in a variable so I can use it for my mySQL select statement.
My comments in the code should explain enough of my problem.
<?php
require_once("connect.php");
$sql="SELECT Name FROM `make`"; //grabs the names of car manufacturers from database
$querymakelist = mysql_query($sql);
$modeloptions="";
$makeoptions="";
while($row = mysql_fetch_array($querymakelist))
{
$id=$row[0];
$makeoptions.="<option value=\"$id\">".$id.'</option>';
}
//the below function, builds the list of models depending on the make chosen for the 2nd dropdown, it all works exept the issue
//stated below. This function is not called until later in the html
function buildModels() {
$sql="SELECT modelname FROM `model` WHERE mfr=\"$modeloptions\";";
//Main issue:
//I need to get the selected value from the first drop down's text or value field and put it into $modeloptions ! How???
//The above line WILL properly grab models from database if I declare what $modeloptions is.
//ex. $modeloptions = "Ford" ... declaring it as static works fine, but I need to declare it dynamically.
$modellist=mysql_query($sql);
while($row = mysql_fetch_array($sql))
{
$modelid=$row[0];
$modeloptions.="<option value=\"$modelid\">".$modelid.'</option>';
}
}
?>
<html>
<head>
<title>FastChange Oil Light Reset</title>
</head>
<body>
<p class="headertext">Choose a Make -> </p>
<form name=mainform>
<SELECT name=make>
<OPTION VALUE=0 onChange="buildModels();" >Select Make
<?=$makeoptions?>
<!-- I NEED TO Grab whatever was chosen here and apply it to the $modeloptions variable in the above php -->
</SELECT>
<SELECT name=model>
<OPTION VALUE=0>Select Model
<?=$modeloptions?>
</SELECT>
</form>
</body>
</html>