Hello All, I'm very new to the world of php, js and ajax however I've been working on creating two combo boxes built from values in a table. The table is such - LeagueCode, TeamCode, TeamName. The first combo displays all distinct LeagueCode values.
The second combo then needs to display the TeamName where LeagueCode equals the value selected above. The method that I'm using works....but only once. Actually, it seems to work in Chrome, Firefox and Opera, but just doesn't work in IE (I'm on version 7). In IE if I select League a second time, it gives me an error of 'Object doesn't support this property or method' at Line 1 Char 1.
Any help and/or guidance is greatly appreciated.
order.htm
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
window.onload = function()
{cmbLeague();}
function cmbLeague(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('cmbLeague');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
//var league = document.getElementById('league').value;
//var queryString = "?league=" + league;
//ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.open("GET", "cmbLeague.php", true);
ajaxRequest.send(null);
}
function cmbTeam(selLeague){
league = '';
alert('selLeague: ' + selLeague);
league = selLeague;
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('cmbTeam');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// var colLeague = document.getElementById('cmbLeague').value;
// alert('byId: ' + colLeague);
var queryString = "?league=" + league;
ajaxRequest.open("GET", "cmbTeam.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<table>
<form name='myForm'>
<tr>
<td class="label">League:</td>
<td>
<div id="cmbLeague"></div>
</td>
</tr>
<tr>
<td class="label">Team:</td>
<td>
<div id="cmbTeam" class="text"></div>
</td>
</tr>
</form>
</table>
</body>
</html>
cmbTeam.php
<?php
require("password.php");
//Connect to MSSQL Server
mssql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mssql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$league = $_GET['league'];
//build query
$query = "SELECT TeamCode, TeamName FROM tblTeam WHERE LeagueCode = '$league'";
//echo $query;
//Execute query
$qry_result = mssql_query($query) or die(mssql_error());
echo "<select name=\"cmbTeam\" id=\"cmbTeam\" class=\"text\" onChange=\"alert(this.value);\">\n";
echo "<option value=\"NULL\">Select Value</option>\n";
// Add an option in the combo for each result
while($row = mssql_fetch_array($qry_result)){
$strA = $row["TeamCode"];
$strB = $row["TeamName"];
echo "<option value=\"$strA\">$strB</option>\n";
}
echo "</select>";
echo "Query: " . $query . "<br />";
?>
cmbLeague.php
<?php
require("password.php");
//Connect to MSSQL Server
mssql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mssql_select_db($dbname) or die(mysql_error());
//build query
$query = "SELECT DISTINCT LeagueCode FROM tblTeam";
//echo $query;
//Execute query
$qry_result = mssql_query($query) or die(mssql_error());
//echo "<select name=\"cmbLeague\" id=\"cmbLeague\" class=\"text\" onChange=\"cmbTeam(this.options[this.selectedIndex].value);\">\n";
echo "<select name=\"cmbLeague\" id=\"cmbLeague\" class=\"text\" onChange=\"cmbTeam(this.value);\">\n";
//echo "<select name=\"cmbLeague\" id=\"cmbLeague\" class=\"text\" onChange=\"alert(this.value);\">\n";
echo "<option value=\"NULL\">-- Select a League --</option>\n";
// Add an option in the combo for each result
while($row = mssql_fetch_array($qry_result)){
$strA = $row["LeagueCode"];
echo "<option value=\"$strA\">$strA</option>\n";
}
echo "</select>";
//echo "Query: " . $query . "<br />";
?>