| | |
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:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2008
Posts: 9
Reputation:
Solved Threads: 0
Ok this is the drop down for the city:
The dropdown looks like this
This is how i get the array with neighborhoods:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<div class="titBox titBoxM"><span class="fontF1 fontWBold fontSzBig"><?=$lang['grad']?>:</span></div> <div class="boxGrad boxGradM"> <center> <?=addDropDown(0,0,0,"gradove",$gradove[$dropN],$lang['emptyDropDown'],'class="szMT2"',$lang['raion'],1,"\n","\n",""); ?> </center> </div>
The dropdown looks like this
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<select name="city"> <option value="0">City 1</option> <option value="1">City 2</option> <option value="2">City 3</option> etc. </select>
This is how i get the array with neighborhoods:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<div class="titBox titBoxM"><span class="fontF1 fontWBold fontSzBig"><?=$lang['kvartala']?>:</span></div> <div class="boxSelect"> <? for ($i=0;$i<sizeof($lang['kvartalS']);$i++){ echo '<input type="checkbox" name="kvartal" value="'.$i.'" /> <span class="fontF1 fontSzBig">'.$lang['kvartalS'][$i].'</span><br />'."\n".' '; } ?> </div>
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.
Again as to how this is achieved using PHP would be best posted in the PHP forums.
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)
<!-- Dynamic drop downs using Javascript Copyright (C) 2008 sos aka Sanjay This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Select Example</title> <script type="text/javascript"> function fillSelBox(srcElem, destName) { var selOne = srcElem; var frm = srcElem.form; var selTwo = frm.elements[destName]; var map = {"fruit": {"default": "- Select -", mango: "Mango", custardApple: "Custard Apple"}, "vegetable": {"default": "- Select -", bitterGourd: "Bitter Gourd", onion: "Onion"}, "fastFood": {"default": "- Select -", noodle: "Noodles", soup: "Soup"}, "default": {"default": "- Select -"} }; selBoxUtil(selOne, selTwo, map); } function selBoxUtil(src, dest, map) { if(!src || !dest || !map) { return; } removeAll(dest); var selected = src.options[src.selectedIndex].value; for(var key in map) { if(key == selected) { var valueMap = map[key]; for(var innerKey in valueMap) { var val = valueMap[innerKey]; addOption(dest, val, innerKey); } } } } function addOption(e, value, label) { if(!e) { return; } var o = new Option(value, label); try { e.add(o); } catch(ee) { e.add(o, null); } } function removeAll(e) { if(e && e.options) { e.options.length = 0; } } </script> </head> <body id="bdy"> <form id="frm" action="#"> <div id="container"> <select name="selOne" id="selOne" onchange="fillSelBox(this, 'selTwo');"> <option value="default">- Select -</option> <option value="fruit">Fruits</option> <option value="vegetable">Vegetables</option> <option value="fastFood">Fast Food</option> </select> <select name="selTwo" id="selTwo"> <option value="default">- Select -</option> </select> </div> </form> </body> </html>
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
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
•
•
Join Date: Apr 2008
Posts: 9
Reputation:
Solved Threads: 0
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
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
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
Assuming this isn't your first stint with Ajax, you can easily process the response using either the
/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
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
![]() |
Similar Threads
- Smart chained select boxes (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Google API Search Engine Code
- Next Thread: Drag and drop text
Views: 2593 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxexample ajaxjspservlets array autoplay beta blackjack boarder box browser button captcha captchaformproblem cart close codes column css date debugger decimal dependent design developer dom download element embed enter error events firefox flash focus form frameworks game gears getselection google gwt gxt hiddenvalue highlightedword hint html ie7 iframe index java javascript javascripthelp2020 javascripts jquery jsp libcurl listbox maps marquee masterpage media menu mp4 onerror onmouseover parameters paypal php player position post problem programming prototype rating redirect safari scale scriptlets search security select size software solutions sources starrating synchronous toggle tweet unicode variables w3c web webkit webservice window windowofwords xml






