Dynamic drop down list not working

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2007
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Dynamic drop down list not working

 
0
  #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
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3.  
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5.  
  6. <script>
  7. function getXMLHTTP() { //fuction to return the xml http object
  8. var xmlhttp=false;
  9. try{
  10. xmlhttp=new XMLHttpRequest();
  11. }
  12. catch(e) {
  13. try{
  14. xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. catch(e){
  17. try{
  18. req = new ActiveXObject("Msxml2.XMLHTTP");
  19. }
  20. catch(e1){
  21. xmlhttp=false;
  22. }
  23. }
  24. }
  25.  
  26. return xmlhttp;
  27. }
  28.  
  29.  
  30.  
  31. function getItem(strURL) {
  32.  
  33. var req = getXMLHTTP();
  34.  
  35. if (req) {
  36.  
  37. req.onreadystatechange = function() {
  38. if (req.readyState == 4) {
  39. // only if "OK"
  40. if (req.status == 200) {
  41. document.getElementById('itemdiv').innerHTML=req.responseText;
  42. } else {
  43. alert("There was a problem while using XMLHTTP:\n" + req.statusText);
  44. }
  45. }
  46. }
  47. req.open("GET", strURL, true);
  48. req.send(null);
  49. }
  50.  
  51. }
  52. </script>
  53.  
  54. <?
  55. require_once('../sqlconnect/connect.php');
  56. ?>
  57.  
  58. </head>
  59.  
  60. <body>
  61.  
  62. <form method="post" action="" name="form1">
  63. <table width="60%" border="0" cellspacing="0" cellpadding="0">
  64. <tr>
  65. <td width="25">Group</td>
  66. <td width="15"><select name="group" onChange="getItem('findcity.php?group='+this.value)">
  67. <option value="">Select Group</option>
  68.  
  69. <?
  70. //make query
  71. $query ="SELECT * FROM groups";
  72. $result = mysql_query($query);
  73. //run
  74.  
  75. $query = mysql_query('SELECT * FROM groups');
  76. if(mysql_error())
  77. {
  78. print(mysql_error());
  79. }
  80.  
  81. while($row = mysql_fetch_array($query))
  82. {
  83. echo '<option value="'.$row["groupname"].' ">'.$row["groupname"];
  84. }
  85.  
  86. ?>
  87.  
  88. </select></td>
  89. </tr>
  90. <tr style="">
  91. <td>Item</td>
  92. <td ><div id="itemdiv"><select name="item">
  93. <option>Select Item</option>
  94. </select></div></td>

findcity.php
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <select name="item">
  2. <option value="">Select Item</option>
  3.  
  4. <?
  5.  
  6. //if so, connect to database
  7. require_once('../sqlconnect/connect.php');
  8.  
  9. $query="SELECT item FROM pricelist WHERE groupname=$groupname";
  10. $result=mysql_query($query);
  11.  
  12. $query = mysql_query('SELECT * FROM pricelist');
  13. if(mysql_error())
  14. {
  15. print(mysql_error());
  16. }
  17.  
  18. while($row = mysql_fetch_array($query))
  19. {
  20. echo '<option value="'.$row["item"].' ">'.$row["item"];
  21. }
  22.  
  23. ?>
  24.  
  25. </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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,382
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 217
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Dynamic drop down list not working

 
0
  #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 y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: Dynamic drop down list not working

 
0
  #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 Quick reply to this message  
Join Date: Apr 2005
Posts: 1,382
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 217
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Dynamic drop down list not working

 
0
  #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 y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: Dynamic drop down list not working

 
0
  #5
Mar 9th, 2008
*bump*
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: leadinnasirawan is an unknown quantity at this point 
Solved Threads: 0
leadinnasirawan leadinnasirawan is offline Offline
Newbie Poster

Re: Dynamic drop down list not working

 
0
  #6
Aug 14th, 2009
@mortalex

Oh Shit!! I got the same problem using ajax... I believe that your coding is perfectly correct. This shit works only the first time page loads

@mortalex This same coding will work very correctly if you use it not in table (in open page or outside of table)

Any other suggestion or solution..?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: leadinnasirawan is an unknown quantity at this point 
Solved Threads: 0
leadinnasirawan leadinnasirawan is offline Offline
Newbie Poster

Re: Dynamic drop down list not working

 
0
  #7
Aug 14th, 2009
@ShanCPlus very funny u are.. :-x
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC