Friday, November 25, 2016

YQL to Populate Country,State and Distrct/County Drop down Based on Selection in parents Drop down!

<html>
  <head><title>YQL and RSS: Yahoo! Top News Stories</title>
  <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  <script src="http://yui.yahooapis.com/3.10.3/build/yui/yui-min.js"></script>
  <script type='text/javascript'>
    /*function top_stories(o){
      var items = o.query.results.item;
      var output = '';
      var no_items=items.length;
      for(var i=0;i<no_items;i++){
        var title = items[i].title;
        var link = items[i].link;
        var desc = items[i].description;
        output += "<h3><a href='" + link + "'>"+title+"</a></h3>" + desc + "<hr/>";
      }*/
      // Place news stories in div tag
 function top_stories(o){   //json output get stored in o
      var items = o.query.results.place; //json array get saved in items
      var output = '';
 var no_items=items.length;  //size of items like no. of countries
      for(var i=0;i<no_items;i++){
 output=output+"<option value=\""+items[i].woeid+ "\">";
 output=output+items[i].name+"</option>";
 }
      document.getElementById('countri').innerHTML = output;  //innerHTML everything between opening and closing element
    }
function updatestate()
{
e=document.getElementById('countri').value;
//http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.states%20where%20place%3D%2223424819%22&format=json&diagnostics=true&callback=cbfunc
qe="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.states%20where%20place%3D"+e+"&format=json&diagnostics=true";
$.ajax( {
    url: qe,
  type: 'POST',
  //data: {"num1":number1,"num2":number2},
    success: function(response) {
obj=response.query.results.place;
var output = '';
  var l=obj.length;
      for(var i=0;i<l;i++){
output=output+"<option value=\""+ obj[i].woeid +"\">";
output=output+obj[i].name;
output=output+'</option>';
}
document.getElementById('state').innerHTML=output;
document.getElementById('county').innerHTML='';
document.getElementById('district').innerHTML='';
}
    });
}

function updatecounty()
{
e=document.getElementById('state').value;
//http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.states%20where%20place%3D%2223424819%22&format=json&diagnostics=true&callback=cbfunc
qe="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.counties%20where%20place%3D"+e+"&format=json&diagnostics=true";
$.ajax( {
    url: qe,
  type: 'POST',
  //data: {"num1":number1,"num2":number2},
    success: function(response) {
obj=response.query.results.place;
var output = '';
  var l=obj.length;
      for(var i=0;i<l;i++){
output=output+"<option value=\""+ obj[i].woeid +"\">";
output=output+obj[i].name;
output=output+'</option>';
}
//alert(output);
document.getElementById('county').innerHTML=output;
document.getElementById('district').innerHTML='';
}
    });
}

function updatedistrict()
{
e=document.getElementById('county').value;
//http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.states%20where%20place%3D%2223424819%22&format=json&diagnostics=true&callback=cbfunc
qe="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.districts%20where%20place%3D"+e+"&format=json&diagnostics=true";
console.log(qe);
$.ajax( {
    url: qe,
  type: 'POST',
  //data: {"num1":number1,"num2":number2},
    success: function(response) {    
                       if(response)
{
obj=response.query.results;
if(obj!=null)
{
obj=obj.place;
var output = '';
  var l=obj.length;
      for(var i=0;i<l;i++){
output=output+"<option value=\""+ obj[i].woeid +"\">";
output=output+obj[i].name;
output=output+'</option>';
}
//alert(output);
document.getElementById('district').innerHTML=output;
}
}
}
    });
}
    </script>
  </head>
  <body>
    <!-- Div tag for stories results -->
    <select id="countri" name="countri" onChange="updatestate()">

</select>
<select id="state" name="state" onChange="updatecounty()"></select>
<select id="county" name="county" onChange="updatedistrict()"> </select>
<select id="district" name="district"></select>
    <!-- The YQL statment will be assigned to src. -->
    <script src='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.countries&format=json&diagnostics=true&callback=top_stories'></script>
  </body>
</html>

No comments:

Post a Comment