Jump to content

dynamic link


muzammil

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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! ;)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.