I’ll start off by saying I’ve been using this example here:
It is formulated for cities/states etc. but I’m just trying to get it to work with my database so I’m pulling employee information out but it still has his city/state text in some places so it may look a little weird.
Also at the moment I’m just trying to get this to work with 2 drop downs, not all 3 for the moment.
Anyways, this is what I have so far, and I’m struggling to get it to work for me. Could anybody point out where I’m going wrong?
Here is what I have in index.php, it’s basically exactly the same I just have the country names swapped out for employee ID’s that are tied to a couple (fake) employee ID’s in my database.
<form method="post" name="form1">
<table border="0" cellpadding="0" cellspacing="0" width="60%"><tbody>
<tr>
<td width="150">Country</td>
<td width="150"><select style="background-color: #ffffa0" name="country" onchange="getState(this.value)">
<option>Select Country</option><option value="1">mg05</option>
<option value="2">aa01</option></select>
</td>
</tr>
<tr>
<td>State</td>
<td>
<p id="statediv">
<select style="background-color: #ffffa0" name="state">
<option>Select Country First</option></select>
</td>
</tr>
</tbody></table>
</form>
This is the javascript at the bottom of index.php which I kicks off the ajax request. Note, in the example he is using a mysql database, I am using ODBC to access an Access database, so keep that in mind.
<script type="text/javascript">
function getState(countryId)
{
var strURL="findState.php?country="+countryId;
var req = getXMLHTTP();
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4)
{
// only if "OK"
if (req.status == 200)
{
document.getElementById('statediv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
Below is what I have in my findState.php file, I had to adjust several things to account for the fact that he used a mysql database and I am using ODBC to get to an Access database.
<?
$country=$_POST['country'];
//make connection to database, bail if no connection
$connection = odbc_pconnect('db','','');
if (!$connection) { exit("Connection Failed: " . $connection); }
//retrieve usernames and passwords
$query = "SELECT (EName) FROM LoginTable WHERE EmployeeID='$country'";
$result = odbc_exec($connection, $query);
?>
<select name="state">
<option>Select State</option>
<? while($row = odbc_fetch_row($result,'EName')){ ?>
<option value=<?=$row['EName']?>><?=$row['EName']?></option> //Error on this line
<? } ?>
</select>
All it’s giving me is that there is an error, Undefined variable: Row on line 22, which I have commented. It displays this error twice, meaning both instances where I reference $row are invalid.
Any help or insights? I’d really appreciate it!!
Add quotes around option value.. check the code below.