Solved

Populate a form dropdown with JSON

Posted on 2013-06-20
2
495 Views
Last Modified: 2013-06-21
Hi

I need to get data from a JSON file located in the root of my website. The data should them populate a dropdown menu.

The setup: ASP page with a form where there are 2 dropdown menu's. When the first changes i need a script that gets the corresponding values from the JSON file.

I have attached a sample JSON file. Imagine that the first dropdown has a selected value of "SAS".



{


"SAC": ["
{ "ddvalue":"8G","ddname":Agent"},"

"SAS": ["
{ "ddvalue":"BZ","ddname":Blizzard"},"
{ "ddvalue":"DY","ddname":Dynastar"},"
{ "ddvalue":"EL","ddname":Elan"},"
{ "ddvalue":"FI","ddname":Fischer"},"
{ "ddvalue":"HE","ddname":Head"},"
{ "ddvalue":"KS","ddname":Kästle"},"
{ "ddvalue":"LI","ddname":Line"},"
{ "ddvalue":"VL","ddname":Völkl"},"



"SBP": ["
{ "ddvalue":"PP","ddname":Pieps"},"


"SFA": ["
{ "ddvalue":"TT","ddname":Tatonka"},"



"SGO": ["
{ "ddvalue":"SY","ddname":Spy"},"

"SHE": ["
{ "ddvalue":"HE","ddname":Head"},"

"SHW": ["
{ "ddvalue":"21","ddname":2117"},"

"SJA": ["
{ "ddvalue":"21","ddname":2117"},"



"SPO": ["
{ "ddvalue":"LK","ddname":Leki"},"


"SSC": ["
{ "ddvalue":"CA","ddname":Cratoni"},"
{ "ddvalue":"PP","ddname":Pieps"},"

"SSO": ["
{ "ddvalue":"SP","ddname":Spyder"},"

"STA": ["
{ "ddvalue":"TT","ddname":Tatonka"},"

"STR": ["
{ "ddvalue":"21","ddname":2117"},"



"SUS": ["
{ "ddvalue":"21","ddname":2117"},"

"SUT": ["
{ "ddvalue":"21","ddname":2117"},"


}

Open in new window

0
Comment
Question by:kbnewgear
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 39261983
Where's the HTML code for the two dropdown?
Please validate your JSON data here : http://jsonlint.com/
Currentlu it's not a valid JSON object!
0
 
LVL 15

Assisted Solution

by:Jagadishwor Dulal
Jagadishwor Dulal earned 250 total points
ID: 39262011
See here the example this is the html page please manage the jquery src file:

<html>
<head>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(function(){
        $.getJSON('mylist.json',function(data){
            console.log('success');
            $.each(data.employees,function(i,emp){
                $('select').append('<option value="'+emp.firstName+' '+emp.lastName+'">'+emp.firstName+' '+emp.lastName+'</option>');
            });
        }).error(function(){
            console.log('error');
        });
    });
</script>
</head>
<body>
    <select name="fullname">
    
    </select>
</body>
</html>

Open in new window


Now mylist.json file save it in the same folder to load:
{"employees" : [{"firstName":"Jagadish","lastName":"Dulal"},
    {"firstName":"John","lastName":"doe"},
    {"firstName":"Diwakar","lastName":"Shrestha"}
]}

Open in new window


Again see css tricks for reference:
http://css-tricks.com/dynamic-dropdowns/
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question