943,887 Members | Top Members by Rank

Ad:
Aug 21st, 2008
0

AJAX chained select

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trevata is offline Offline
9 posts
since Apr 2008
Aug 22nd, 2008
0

Re: AJAX chained select

How about showing what you have attempted so far?
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Aug 22nd, 2008
0

Re: AJAX chained select

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>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trevata is offline Offline
9 posts
since Apr 2008
Aug 22nd, 2008
0

Re: AJAX chained select

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Aug 22nd, 2008
0

Re: AJAX chained select

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trevata is offline Offline
9 posts
since Apr 2008
Aug 22nd, 2008
0

Re: AJAX chained select

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Aug 22nd, 2008
0

Re: AJAX chained select

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trevata is offline Offline
9 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Google API Search Engine Code
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Drag and drop text





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC