AJAX chained select

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

Join Date: Apr 2008
Posts: 9
Reputation: trevata is an unknown quantity at this point 
Solved Threads: 0
trevata trevata is offline Offline
Newbie Poster

AJAX chained select

 
0
  #1
Aug 21st, 2008
Hi ! I need a javascript code which sends variable to PHP.
I have a drop down menu (<select>) in the select there are diffrent cities and i have an array of neighborhoods and when one city is selected, a part of that array shows up, e.g. from 13 to 28
I have the php code, just need the javascript that sends the variable depending on the selected city.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 471
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: AJAX chained select

 
0
  #2
Aug 22nd, 2008
How about showing what you have attempted so far?
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 9
Reputation: trevata is an unknown quantity at this point 
Solved Threads: 0
trevata trevata is offline Offline
Newbie Poster

Re: AJAX chained select

 
0
  #3
Aug 22nd, 2008
Ok this is the drop down for the city:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div class="titBox titBoxM"><span class="fontF1 fontWBold fontSzBig"><?=$lang['grad']?>:</span></div>
  2. <div class="boxGrad boxGradM">
  3. <center>
  4. <?=addDropDown(0,0,0,"gradove",$gradove[$dropN],$lang['emptyDropDown'],'class="szMT2"',$lang['raion'],1,"\n","\n",""); ?>
  5. </center>
  6. </div>

The dropdown looks like this
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <select name="city">
  2. <option value="0">City 1</option>
  3. <option value="1">City 2</option>
  4. <option value="2">City 3</option>
  5. etc.
  6. </select>

This is how i get the array with neighborhoods:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div class="titBox titBoxM"><span class="fontF1 fontWBold fontSzBig"><?=$lang['kvartala']?>:</span></div>
  2. <div class="boxSelect">
  3. <?
  4. for ($i=0;$i<sizeof($lang['kvartalS']);$i++){
  5. echo '<input type="checkbox" name="kvartal" value="'.$i.'" /> <span class="fontF1 fontSzBig">'.$lang['kvartalS'][$i].'</span><br />'."\n".' ';
  6. }
  7. ?>
  8. </div>
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 471
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: AJAX chained select

 
0
  #4
Aug 22nd, 2008
I don't know PHP so I won't be making any erroneous assumptions here but achieving this depends on how you are pulling the data irrespective of the server side language used. Are you pulling the *entire* data in a single request and toggling the SELECT boxes using Javascript or are you fetching the neighborhood data *on demand*.

No matter the approach, it should be pretty easy if you pass the data from PHP to Javscript in JSON format. Here is a pure Javascript implementation which should push you in the right direction.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!--
  2. Dynamic drop downs using Javascript
  3. Copyright (C) 2008 sos aka Sanjay
  4.  
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. -->
  18. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  19. "http://www.w3.org/TR/html4/strict.dtd">
  20. <html>
  21. <head>
  22. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  23. <title>Select Example</title>
  24. <script type="text/javascript">
  25. function fillSelBox(srcElem, destName) {
  26. var selOne = srcElem;
  27. var frm = srcElem.form;
  28. var selTwo = frm.elements[destName];
  29. var map =
  30. {"fruit":
  31. {"default": "- Select -", mango: "Mango", custardApple: "Custard Apple"},
  32. "vegetable":
  33. {"default": "- Select -", bitterGourd: "Bitter Gourd", onion: "Onion"},
  34. "fastFood":
  35. {"default": "- Select -", noodle: "Noodles", soup: "Soup"},
  36. "default":
  37. {"default": "- Select -"}
  38. };
  39. selBoxUtil(selOne, selTwo, map);
  40. }
  41.  
  42. function selBoxUtil(src, dest, map) {
  43. if(!src || !dest || !map) {
  44. return;
  45. }
  46. removeAll(dest);
  47. var selected = src.options[src.selectedIndex].value;
  48. for(var key in map) {
  49. if(key == selected) {
  50. var valueMap = map[key];
  51. for(var innerKey in valueMap) {
  52. var val = valueMap[innerKey];
  53. addOption(dest, val, innerKey);
  54. }
  55. }
  56. }
  57. }
  58.  
  59. function addOption(e, value, label) {
  60. if(!e) {
  61. return;
  62. }
  63. var o = new Option(value, label);
  64. try {
  65. e.add(o);
  66. } catch(ee) {
  67. e.add(o, null);
  68. }
  69. }
  70.  
  71. function removeAll(e) {
  72. if(e && e.options) {
  73. e.options.length = 0;
  74. }
  75. }
  76. </script>
  77. </head>
  78. <body id="bdy">
  79. <form id="frm" action="#">
  80. <div id="container">
  81. <select name="selOne" id="selOne" onchange="fillSelBox(this, 'selTwo');">
  82. <option value="default">- Select -</option>
  83. <option value="fruit">Fruits</option>
  84. <option value="vegetable">Vegetables</option>
  85. <option value="fastFood">Fast Food</option>
  86. </select>
  87. <select name="selTwo" id="selTwo">
  88. <option value="default">- Select -</option>
  89. </select>
  90. </div>
  91. </form>
  92. </body>
  93. </html>
Again as to how this is achieved using PHP would be best posted in the PHP forums.
Last edited by ~s.o.s~; Aug 22nd, 2008 at 6:51 am.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 9
Reputation: trevata is an unknown quantity at this point 
Solved Threads: 0
trevata trevata is offline Offline
Newbie Poster

Re: AJAX chained select

 
0
  #5
Aug 22nd, 2008
No no man, can AJAX send two variables to the PHP, one with the starting number and second with the finishing number....
Something like this if City 1 is selected the script sends var1=0 and var2=89, or if City 2 is selected the script sends var1=90 and var2=110 so the php script can get these two variables and get the neighborhoods from the array and display them
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 471
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: AJAX chained select

 
0
  #6
Aug 22nd, 2008
Yes, you can send more than one variables to your PHP script residing on the server. Just append the start and end points as request parameters to the query string which you would be using to make an Ajax request. Something like /php/process.php/?start=0&end=10 . Also make sure you encode the URL before making an Ajax request for proper encoding of special characters (like space for instance). And in process.php, you can retrieve the variables from the request object. In J2EE, it would be something like request.getParameter("start") .

Assuming this isn't your first stint with Ajax, you can easily process the response using either the responseText or responseXML attribute of XMLHttpRequest object.
Last edited by ~s.o.s~; Aug 22nd, 2008 at 7:36 am.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 9
Reputation: trevata is an unknown quantity at this point 
Solved Threads: 0
trevata trevata is offline Offline
Newbie Poster

Re: AJAX chained select

 
0
  #7
Aug 22nd, 2008
Thanks man.... I think i can do that.. If I need help I'll write here. Thanks again
Last edited by trevata; Aug 22nd, 2008 at 9:30 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC