Jump to content

Populating Multiple Lists


fanfavorite

Recommended Posts

I am trying to use ajax to populate multiple lists.

 

I can get it to work like this: 

 

<form enctype="multipart/form-data" action="" method="POST">
<select name="league" onchange="showSeason(this.value,'<? echo $_SESSION[ftp] ?>')">
<? $r = mysql_query("select * from League");
$count = 1;
while($g=mysql_fetch_array($r)) {
?><option <? if ($g[Name] == $_POST[league]) { echo " selected"; } ?> value="<? echo $g[Name] ?>"><? echo $g[Name] ?></option>
<?
if ($count == 1) {
$lg = $g[Name];
} 
$count++;	
} ?></select>
<div id="season">     
<select name="season">
<? $r = mysql_query("select * from Seasons WHERE League = '$lg'");
while($g=mysql_fetch_array($r)) {
?><option <? if ($g[season] == $_POST[season]) { echo " selected"; } ?> value="<? echo $g[season] ?>"><? echo $g[season] ?></option>
<? } ?></select></div>
</form>

 

However I need to add more lists:

 

<form enctype="multipart/form-data" action="" method="POST">
<elect name="league" onchange="showSeason(this.value,'<? echo $_SESSION[ftp] ?>');showDivision(this.value,this.form.season.value,'<? echo $_SESSION[ftp] ?>')">
<? $r = mysql_query("select * from League");
$count = 1;
while($g=mysql_fetch_array($r)) {
?><option <? if ($g[Name] == $_POST[league]) { echo " selected"; } ?> value="<? echo $g[Name] ?>"><? echo $g[Name] ?></option>
<?
if ($count == 1) {
$lg = $g[Name];
} 
$count++;	
} ?></select>
<div id="season">
<select name="season" onchange="showDivision(this.form.league.value,this.value,'<? echo $_SESSION[ftp] ?>')">
<? $r = mysql_query("select * from Seasons WHERE League = '$lg'");
$count = 1;
while($g=mysql_fetch_array($r)) {
?><option <? if ($g[season] == $_POST[season]) { echo " selected"; } ?> value="<? echo $g[season] ?>"><? echo $g[season] ?></option>
<?
if ($count == 1) {
$ss = $g[season];
} 
$count++;
} ?></select></p></div>
<div id="div">
<select name="division">
<? $r = mysql_query("select * from Divisions WHERE League = '$lg' AND Season = '$ss'");
while($g=mysql_fetch_array($r)) {
?><option <? if ($g[Division] == $_POST[division]) { echo " selected"; } ?> value="<? echo $g[Division] ?>"><? echo $g[Division] ?></option>
<? } ?></select></div>

 

Now that seems to work sometimes, but not all the time.  The other code is:

 

var xmlHttp;

function showSeason(value,domain) { 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request");
return;
}
var url="sports/js/change.php";
url=url+"?league="+value;
url=url+"&domain="+domain;
url=url+"&call=season";
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function () {
stateChange('season');
};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function showDivision(league,season,domain) { 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request");
return;
}
var url="sports/js/change.php";
url=url+"?league="+league;
url=url+"&season="+season;
url=url+"&domain="+domain;
url=url+"&call=div";
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function () {
stateChange('div');
};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function showTeam(league,season,div,domain) { 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request");
return;
}
var url="sports/js/change.php";
url=url+"?league="+league;
url=url+"&season="+season;
url=url+"&div="+div;
url=url+"&domain="+domain;
url=url+"&call=hometeams";
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function () {
stateChange('home');
};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function showTeam2(league,season,div,domain) { 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request");
return;
}
var url="sports/js/change.php";
url=url+"?league="+league;
url=url+"&season="+season;
url=url+"&div="+div;
url=url+"&domain="+domain;
url=url+"&call=visitteams";
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function () {
stateChange('visit');
};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function showRink(value,domain) { 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request");
return;
}
var url="sports/js/change.php";
url=url+"?value="+value;
url=url+"&domain="+domain;
url=url+"&call=rink";
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function () {
stateChange('rink');
};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChange(field) 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
document.getElementById(field).innerHTML=xmlHttp.responseText;
} 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
}
return xmlHttp;
}

 

<?
$conninc = "../../clients/".$_GET[domain]."/connection.php";
include ($conninc);
if ($_GET["call"] == "season") {
?>
		<p class="forms2">          
		<select name="season">
			<? $r = mysql_query("select * from Seasons WHERE League = '$_GET[league]'");
                                   while($g=mysql_fetch_array($r)) {
                			?><option <? if ($g[season] == $_POST[season]) { echo " selected"; } ?> value="<? echo $g[League] ?>"><? echo $g[season] ?></option>
            			<? } ?></select></p>
<?
} elseif ($_GET["call"] == "div") {
?>
		<p class="forms2">          
		<select name="division">
			<? $r = mysql_query("select * from Divisions WHERE League = '$_GET[league]' AND Season = '$_GET[season]'");
                                   while($g=mysql_fetch_array($r)) {
                			?><option <? if ($g[Division] == $_POST[division]) { echo " selected"; } ?> value="<? echo $g[Division] ?>"><? echo $g[Division] ?></option>
            			<? } ?></select></p>
<?
} elseif ($_GET["call"] == "visitteams") {
?>
		<p class="forms2">          
		<select name="visitingteam">
			<? $r = mysql_query("select * from Teams WHERE League = '$_GET[league]' AND Season = '$_GET[season]' AND Division = '$_GET[div]'");
                                   while($g=mysql_fetch_array($r)) {
                			?><option <? if ($g[Team] == $_POST[team]) { echo " selected"; } ?> value="<? echo $g[Team] ?>"><? echo $g[Team] ?></option>
            			<? } ?></select></p>
<?
} elseif ($_GET["call"] == "hometeams") {
?>
		<p class="forms2">          
		<select name="hometeam">
			<? $r = mysql_query("select * from Teams WHERE League = '$_GET[league]' AND Season = '$_GET[season]' AND Division = '$_GET[div]'");
                                   while($g=mysql_fetch_array($r)) {
                			?><option <? if ($g[Team] == $_POST[team]) { echo " selected"; } ?> value="<? echo $g[Team] ?>"><? echo $g[Team] ?></option>
            			<? } ?></select></p>
<?
} elseif ($_GET["call"] == "rink") {
?>
		<p class="forms2">          
		<select name="rink">
			<? $r = mysql_query("select * from Locations WHERE Name = '$_GET[value]'");
			   $g=mysql_fetch_array($r);
			   for($count = 1;$count<=$g[Rink];$count++) {
               			?><option <? if ($count == $_POST[rink]) { echo " selected"; } ?>><? echo $count ?></option>
            			<? } ?></select></p>
<?
}
?>

 

I am sure there is a much better way of doing this, but I am struggling here.  Any help is appreciated.  Thanks! 

Link to comment
Share on other sites

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.