0

I am getting one value by JSON array, but how can I store multiple values in JSON array and how can I retrieve it by JavaScript?

auto.jsp:

 <script type="text/javascript">
 $(document).ready(function() {
    $("#combo").change(function() { // after onchange event it goes to combo1.jsp 
        $.getJSON('combo1.jsp', {
            count: this.value
        }, function(responseData) {
            var splitValues = responseData.name.split(/,/);

            $("#combo1").empty().append("<option>please select</option>");


            for (var idx in splitValues) {
                $("#combo1").append(
                $("<option></option>").html(splitValues[idx]).val(splitValues[idx]));
            }
        });

    });
});​ 


    </script>

<body>
//first combo box
<select id="combo" name="count">

     <option value="">please select</option>

      <option value="a">A</option>

 </select> 

//second combo box

<select id="combo1" name="combo1Val">

     // i am getting only "5" here, but i want to show 1,2,3,4,5 as in drop down list

 </select> 
</body>

combo1.jsp:

<%
String count=request.getParameter("count");// by onchange event of first combo, i am 
getting value "a" here
if(count.equalsIgnoreCase("a")){
// in my db there are 5 values i.e. 1,2,3,4,5 for single value "a", but while   
populating in second combo i am getting only one value "5", how? please ignore my db   
connection in jsp

  JSONObject arrayObj= new JSONObject(); 
// by db connection i am fetching 5 values but while printing in javascript i am  
getting only last one that is "5" in second combo, how can i populate all values  
1,2,3,4,5 as drop down items in second combo box?
// retrieveing 5 datas(1,2,3,4,5) from db where name =a
 while(rs.next()){
      t1=(String)(rs.getString(1));// there are 5 values in db relating to "a", but i  
am getting only last value i.e. "5" in second combo
       }
       arrayObj.put("name",t1);
          response.setContentType("application/json");
      response.getWriter().write(arrayObj.toString());
      }
%>
vaultah
  • 44,105
  • 12
  • 114
  • 143
harry
  • 731
  • 10
  • 23
  • are you not interested to check the result with console.log(responseData)? – bondythegreat Feb 12 '12 at 11:34
  • You have to use a `JSONArray` and add an item to it within the `while(rs.next()){` loop (i.e. 5 times). – The Nail Feb 12 '12 at 11:34
  • See also this question: *How to generate dynamic drop down lists using jQuery and jsp?* http://stackoverflow.com/questions/2896730/how-to-generate-dynamic-drop-down-lists-using-jquery-and-jsp – The Nail Feb 12 '12 at 11:37
  • Thanks, but I'm flagging this as a duplicate, since what you want to do is exactly the same (only different trigger, on load vs. on change). – The Nail Feb 12 '12 at 11:40
  • @TheNail no please do not flag it as duplicate, my question seldom matches with that question, i am using jsp not jstl tags – harry Feb 12 '12 at 11:43

1 Answers1

1

I think you are getting only 5 in your second combo because you are making an error in your loop. You could do

//in combo1.jsp

String selectedValue = request.getParameter("count");

Map<String, String> options = new Map<String, String>();
 //get your data from db
while(rs.next()){
    String t1=(String)(rs.getString(1));
    options.add(t1, t1);
}
String json = new Gson().toJson(options);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);


    //to get data
    $.getJSON('combo1.jsp', {
        count: this.value
    }, function(options) {
        var dropdown2 = $('#combo1');
        $('>option', dropdown2).remove(); // Clean old options first.
        if (options) {
            $.each(opts, function(key, value) {
                dropdown2.append($('<option/>').val(key).text(value));
            });
        } else {
            dropdown2.append($('<option/>').text("Please select dropdown1"));
        }
    });
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • thanks a lot Nicola for the answer,ya i am getting only 5 in second combo, but 3 errors are coming. 1st: **Map options = new Map;** telling that '(' or '[' expected. 2nd:**options.add(t1, t1);** telling that cannot find symbol method add. 3rd : **String json = new Gson().toJson(options);** telling that cannot find symbol class Gson. I think slight modification is required in your code, you are almost done – harry Feb 13 '12 at 09:57
  • please help me Nicola by editing your code, i was trying but could not do it. – harry Feb 13 '12 at 10:05
  • @harry sorry i edited my code i forgot ()..for the other error you must import Gson i think – Nicola Peluchetti Feb 13 '12 at 11:31
  • @Nicola..please have a look at [this question](http://stackoverflow.com/questions/9277695/insert-data-into-textbox-when-checkbox-is-checked/9277746#9277746) If possible please give an idea – harry Feb 14 '12 at 13:40