muzammil Posted May 12, 2007 Share Posted May 12, 2007 hi i am using mysql as data base, i have three table, country, state, city, i want to show respective satate when a country selected in drop down menu and similar in case of satate and city. i am very poor in php if some one give me exact php code i appriciate. if u need i can u fields name hope i get solution for this. Quote Link to comment Share on other sites More sharing options...
ryeman98 Posted May 12, 2007 Share Posted May 12, 2007 Your English is awful, no offense. Mostly we help with code that is already written but... I don't understand what you mean. Show a respective state when a country is selected in a drop down menu? Quote Link to comment Share on other sites More sharing options...
muzammil Posted May 12, 2007 Author Share Posted May 12, 2007 i mean when US is selected in first drop down menu, second drop down menu should show only US states, if UK is selected it shows only UK states. if in second drop down New York is selected it in third drop down its shows only New York sates cities. Quote Link to comment Share on other sites More sharing options...
freakus_maximus Posted May 12, 2007 Share Posted May 12, 2007 You can do this with javascript. Check out this link. http://www.javascripttoolbox.com/lib/dynamicoptionlist/ Quote Link to comment Share on other sites More sharing options...
muzammil Posted May 12, 2007 Author Share Posted May 12, 2007 i want to bring the data from data base, this will work?? we cant do in php?? Quote Link to comment Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 PHP is server side, in order to change things based upon user selection you must use javascript which is client side. You can mix the 2 using ajax though AJAX is Asynchronous Javascript And Xml It allows you to run server side code by using javascript so there are no page refreshes. You can do it in PHP, but you would need to have your user hit submit after each selection. That is not very efficient so go the AJAX route if you want to accomplish this Quote Link to comment Share on other sites More sharing options...
ryeman98 Posted May 12, 2007 Share Posted May 12, 2007 PHP is server side, in order to change things based upon user selection you must use javascript which is client side. You can mix the 2 using ajax though AJAX is Asynchronous Javascript And Xml It allows you to run server side code by using javascript so there are no page refreshes. You can do it in PHP, but you would need to have your user hit submit after each selection. That is not very efficient so go the AJAX route if you want to accomplish this I thought that would work but I wasn't too sure so I didn't say anything. AJAX is great! Quote Link to comment Share on other sites More sharing options...
muzammil Posted May 12, 2007 Author Share Posted May 12, 2007 how to use ajax?? actaully i post this question in ajax yesterday but no reply me. any way can help me?? Quote Link to comment Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 Asking how to use ajax is as vague and complex as asking how to build an internal combustion engine and what does every thing do. Take a look at this post I made earlier http://www.phpfreaks.com/forums/index.php/topic,140235.msg596486.html#msg596486 It is a complete ajax script and is really simple to use. You will have to figure out how to apply it to your needs. Nate Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted May 12, 2007 Share Posted May 12, 2007 Check out this site this is exactly what u want.but this is in JS not in ajax. http://www.plus2net.com/php_tutorial/php_drop_down_list3.php Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted May 12, 2007 Share Posted May 12, 2007 ok: if all you want is to select country, then automatically give appropriate options for state, then city etc.: then just have an ID in your database, so that your table of states, has another field, for which country, and so forth for all the other tables: then you displaythem all into javascript arrays or something. then use an onclick with the first menu, and even make the other menus unavailable till an onclick on the first menu, and for each menu, use something like: onclick = function1() function function1() { var menu1_value = document.forms[0].menu1.value document.forms[0].menu2.options = options2_array } or somethign like that i cant really come up with the full script here, however if you still need to get it, i coudl do it for you. gimlie_legs@msn.com. gdlk Quote Link to comment Share on other sites More sharing options...
muzammil Posted May 12, 2007 Author Share Posted May 12, 2007 i use java script but problem is that its re-loading the page, which taking lot of time and its clears all the fields too. hope we get solution for this, what u ppl think of ajax, Quote Link to comment Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 what u ppl think of ajax, ummmmmmm... its only been mentioned several times. and I provided a link to a post I did that has a complete ajax handler that can be called using any of the onXXX events. onclick onblur onchange etc... So I think the general consensus is that it's your best bet to do what your wanting to do. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 12, 2007 Share Posted May 12, 2007 AJAX is a possibility, but in my opinion, if you don't have a very large list of options to be dynamically loaded I prefer to do it all in javascript as it will be much faster for the client and it will put less load on the server. Here is an example of how you would do it in just javascript, but if you have to much data to load to the page, then you could use the same functionality with AJAX and get the list from the server each time the user changes a select list: <html> <head> <script type="text/javascript"> //Create array for state lists stateLists = new Array () // Create sub-arrays for each country // in same order as in select list stateLists[0] = new Array ('California','Florida','New York'); stateLists[1] = new Array ('Alberta','British Columbia','Manitoba'); stateLists[2] = new Array ('Aguascalientes','Chiapas','Hidalgo'); //Create array for state lists cityLists = new Array () // Create sub-arrays for each country // and sub-arrays for each state // in same order as in select list cityLists[0] = new Array (); //USA cityLists[0][0] = new Array ('Los Angeles','San Diego','San Francisco'); //California cityLists[0][1] = new Array ('Ft. Lauderdale','Miami','Orlando'); //Florida cityLists[0][2] = new Array ('Albany','Camden','New York'); //New Yourk cityLists[1] = new Array (); //Canada cityLists[1][0] = new Array ('Calgary','Edmonton','Lacombe'); //Alberta cityLists[1][1] = new Array ('Clearwater','North Vancouver','Port Alberni'); //British Columbia cityLists[1][2] = new Array ('Cranberry Portage','Steinbach','Stonewall'); //Manitoba cityLists[2] = new Array (); //Mexico cityLists[2][0] = new Array ('Calvillo','Pabellón de Arteaga','Rincón de Romos'); //Aguascalientes cityLists[2][1] = new Array ('Cintalapa','Palenque','Tapachula'); //Chiapas cityLists[2][2] = new Array ('Huejutla de Reyes','Tepeji de Ocampo','Tizayuca'); //Hidalgo function setStateList() { cntryFld = document.getElementById('country'); stateFld = document.getElementById('state'); if (!cntryFld.value) { stateFld.options.length=0; stateFld.options[0] = new Option('--Select Country--',''); stateFld.disabled = true; } else { stateFld.options.length=0; stateList = stateLists[cntryFld.selectedIndex-1]; for(i=0; i<stateList.length; i++) { stateFld.options[i] = new Option(stateList[i],stateList[i]); } stateFld.disabled = false; } setCityList(); } function setCityList() { cntryFld = document.getElementById('country'); stateFld = document.getElementById('state'); cityFld = document.getElementById('city'); if (!stateFld.value) { cityFld.options.length=0; cityFld.options[0] = new Option('--Select Country--',''); cityFld.disabled = true; } else { cityFld.options.length=0; cityList = cityLists[cntryFld.selectedIndex-1][stateFld.selectedIndex]; for(i=0; i<cityList.length; i++) { cityFld.options[i] = new Option(cityList[i],cityList[i]); } cityFld.disabled = false; } } </script> </head> <body> Country: <select name="country" id="country" onchange="setStateList();"> <option value="">--Select Country--</option> <option value="USA">USA</option> <option value="Canada">Canada</option> <option value="Mexico">Mexico</option> </select><br> State: <select name="state" id="state" onchange="setCityList();" disabled> <option value="">--Select Country--</option> </select></br> City: <select name="city" id="city" disabled> <option value="">--Select Country--</option> </select> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.