User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 374,007 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,907 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1303 | Replies: 4
Reply
Join Date: Oct 2007
Location: England
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Troubleshooting Dynamic drop down list not working

  #1  
Mar 7th, 2008
Hey,

I'm trying to create a dynamic drop down box where the first drop down box populates the second drop down, but it isn't working. I have two tables;
pricelist with fields,
-id
-groupname
-item
-price
groups with fields,
-id
-groupname

I've got the code and to me it looks as though it completely works, but i think its the fact that two of the fields have the same name, and it's causing problems.

index.php
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script>
function getXMLHTTP() { //fuction to return the xml http object
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				req = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
		 	
		return xmlhttp;
	}
	
	
	
	function getItem(strURL) {		
		
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('itemdiv').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}
				
	}
</script>

<?
require_once('../sqlconnect/connect.php');
?>

</head>

<body>

<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="25">Group</td>
    <td  width="15"><select name="group" onChange="getItem('findcity.php?group='+this.value)">
	<option value="">Select Group</option>
	
<?
//make query
	$query ="SELECT * FROM groups";
	$result = mysql_query($query);
//run

$query = mysql_query('SELECT * FROM groups');
if(mysql_error())
{
print(mysql_error());
}

while($row = mysql_fetch_array($query)) 
{
echo '<option value="'.$row["groupname"].'  ">'.$row["groupname"];
} 

?>

        </select></td>
  </tr>
  <tr style="">
    <td>Item</td>
    <td ><div id="itemdiv"><select name="item">
	<option>Select Item</option>
        </select></div></td>
 

findcity.php
<select name="item">
<option value="">Select Item</option>

<? 

//if so, connect to database
	require_once('../sqlconnect/connect.php');

$query="SELECT item FROM pricelist WHERE groupname=$groupname";
$result=mysql_query($query);

$query = mysql_query('SELECT * FROM pricelist');
if(mysql_error())
{
print(mysql_error());
}

while($row = mysql_fetch_array($query)) 
{
echo '<option value="'.$row["item"].'  ">'.$row["item"];
} 

?>

</select>

When i select an option in the first drop down box, it populates the second with all the values in the table, not just the selected ones.

Cheers
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2005
Location: New York state
Posts: 442
Reputation: ShawnCplus will become famous soon enough ShawnCplus will become famous soon enough 
Rep Power: 5
Solved Threads: 65
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Dynamic drop down list not working

  #2  
Mar 7th, 2008
Well for both files you aren't ending your option tag when you generate them, it may not be what's causing the problem but it's incorrect nonetheless
GCS d- s+:+ a-->? C++(++++) UL+++ P+>+++ L+++ !E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r z+*
Reply With Quote  
Join Date: Oct 2007
Location: England
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: Dynamic drop down list not working

  #3  
Mar 7th, 2008
Yea i know, i purposely left them off, to save space.
What other problems have you seen with my code?
Reply With Quote  
Join Date: Apr 2005
Location: New York state
Posts: 442
Reputation: ShawnCplus will become famous soon enough ShawnCplus will become famous soon enough 
Rep Power: 5
Solved Threads: 65
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Dynamic drop down list not working

  #4  
Mar 7th, 2008
Sorry, I actually laughed at that. Leaving off the end tag makes the HTML invalid, and can cause cascading errors.
GCS d- s+:+ a-->? C++(++++) UL+++ P+>+++ L+++ !E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r z+*
Reply With Quote  
Join Date: Oct 2007
Location: England
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: Dynamic drop down list not working

  #5  
Mar 9th, 2008
*bump*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 10:39 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC