Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Suppose we have two dropdown
dropdown1 for country and dropdown2 for state

i want to show the states in dropdown2 on dropdown1 change event(when we select any country then show states of particular country in dropdown2 from database)?
Posted

Hi Again,
Here's an Example... A Complete Test Page with this feature
File: index.html

HTML
<!doctype html>
<html>
<head>
 <meta charset="utf-8">
 <title>Representation of AJAX</title>
 <script type="text/javascript">
   function update(str)
   {
      var xmlhttp;

      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
      else
      {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }	

      xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
          document.getElementById("data").innerHTML = xmlhttp.responseText;
        }
      }

      xmlhttp.open("GET","demo.php?opt="+str, true);
      xmlhttp.send();
  }
</script>

</head>
<body>
  <select id="optionA" onchange="update(this.value)">
    <option value="0">Select...</option>
    <option value="1">Option1</option>
    <option value="2">Option2</option>
  </select>
  <br/>
  <select id="data">
    <option>Select an Option...</option>
  </select>
</body>
</html>


File: demo.php 

PHP
<?php
  $opt = $_GET['opt'];

  switch($opt)
  {
    case 0:
    default:
      echo '
            <option>Select an Option...</option>
           ';
        break;
    case 1:
      echo '
            <option value="opt1_1">Option1_Test1</option>
            <option value="opt1_2">Option1_Test2</option>
            <option value="opt1_3">Option1_Test3</option>
           ';
        break;
    case 2:
      echo '
            <option value="opt2_1">Option2_Test1</option>
            <option value="opt2_2">Option2_Test2</option>
            <option value="opt2_3">Option2_Test3</option>
           ';
        break;
  }
?>

Hope that this example will help.

With Regards
Tushar Srivastava
 
Share this answer
 
v2
Comments
Mr.Venus 17-Dec-13 4:52am    
Thank you very much Tushar sriv . I am a Newbie in coding :( So I cannot understand ajax,php codings.. anyway Thanks for your help. I have tested this on my computer. It worked. I am very happy now. Kindly give me some learning resources for programming
Er. Tushar Srivastava 17-Dec-13 7:03am    
It is a pleasure to help peoples :) I am always here to help. Well for learning resources on internet, I would like to tell you two things. If you retain or learn better by audio visual source then you can go for video tutorials on youtube or net.tutsplus but if you are more comfortable in reading and understanding than no one is better friend's of yours than your favorite books :) Enjoy learning and always challange yourself to do big. Its how I have learned :) Best of Luck
Mr.Venus 18-Dec-13 1:22am    
Thank you very much dude :) And also I have one doubt, U have used switch case in php from above example. Is it possible to use that by using arrays? If yes ? just give me a example of full code. Thank u once again
Er. Tushar Srivastava 18-Dec-13 5:07am    
Actually it is possible but I am sure the code will look seriously clumsy using that technique 'cause then you have to build an array containing the code as element and then you will be using the index received via a GET request to echo that element. It is better to stick to this solution :) BTW, you can try doing what I have given you as a homework hint ;) Best of Luck Buddy!!!
Mr.Venus 20-Dec-13 0:20am    
Tnank u :( I have tried that by using ararays. Still I was stuck with them. There are so many types of arrays. I dont know which one is efficient to above scenerio.Kindly give me the demo of php arrray for above scenerio
Well, My Friend, I am sure to help you since I have already replied to such a question previously too.
Let us see, how to do it...

1. First thing you need to think is, how will it be updating... ie. will the page reload or will it update automatically (obviously we will be going with the second one).

2. So, we are going with second one, we will need AJAX to do so.... Now it's your job to implement it. i can only give you idea.

3. The next thing is, make a page which can be called as processor, whenever a the name of country is submitted to this page, (you will be doing so using ajax) it will return you will a list of name of states. See the codes below..(sorry, it is in php but I am sure you can implement it for ASP as well)
PHP
foreach($array as $statename)
{
 echo "<option>".$statename."</option>";
}


This way the data is now recieved by you... Now paste this data inside the dropdown list using ajax as..
JavaScript
document.getElementByID("statelist") = xmlhttp.responseText;


Now here will be your codes inside the form:
HTML
<select>
<div id="statelist">
<option>This will be replaced by the state names</option>
</div>
</select>


Hope that this will help you...

Regards
Tushar Srivastava :-)
 
Share this answer
 
Comments
Mr.Venus 12-Dec-13 6:38am    
Hey bro !! I need full source code of this. I was stuck with same issue. U already given solution.Though I need full source of html ,ajax,php. Thanks in advance
Er. Tushar Srivastava 16-Dec-13 8:04am    
All right my friend..... I will be posting the reply soon.... sorry for the delay... I was busy with my exams :)
Er. Tushar Srivastava 16-Dec-13 8:50am    
I have posted a similar example as an answer below... Check it... Hope that it will help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900