Jquery ajax populate dropdown with json response data

aDvo

I know there are quite a few questions floating on this but I'm still not sure what to do.

I have a HTML form called "CuisineForm", after the user selects the type of cuisine, the AJAX sends the form to the server. The AJAX call works fine, and server responds with a JSON responsewhich contains all the serving times of this particular cuisine. These serving times are divided into Breakfast, Lunch and Dinner.

These times need to be populated into 3 separate dropdowns in the same form. but I don't really know how to process the JSON results to populate the 3 dropdowns in the form.

Here is the JSON response from the PHP server side script. Note that this was generated in the PHP script using "echo json_encode()".

{"breakfast":[{"09:00:00":"9:00 am"},{"09:30:00":"9:30 am"}],"lunch":[{"12:00:00":"12:00 pm"},{"12:30:00":"12:30 pm"}],"dinner":[{"19:00:00":"7:00 pm"},{"19:30:00":"7:30 pm"}]}

Here is my $.post code.

$.post( "gettimeslots", $( "#CuisineForm" ).serialize(), function() {
          alert( "success" );
        })
          .done(function(data) {
            // Not sure what code to write here to populate dropdown

          })
          .fail(function() {
            alert( "There was a problem getting the dropdown values!" );
          })
          .always(function() {
            alert( "always" );
        });

Here is my dropdown that I want to populate

<select name="breakfasttimes" id="breakfasttimes"></select>
<select name="lunchtimes" id="lunchtimes"></select>
<select name="dinnertimes" id="dinnertimes"></select>

Most implementations tend to use $.getJSON. But as shown above I am using $.post instead. Please tell me if I should use $.getJSON instead.

Can someone offer some pointers or code advice?

Thanks Kevin

Ofer Haber

Heres a fiddle that contains the full answer

HTML:

<select name="breakfast" id="breakfast"></select>
<select name="lunch" id="lunch"></select>
<select name="dinner" id="dinner"></select>

JS:

var data = {"breakfast":[{"09:00:00":"9:00 am"},{"09:30:00":"9:30 am"}],"lunch":        [{"12:00:00":"12:00 pm"},{"12:30:00":"12:30 pm"}],"dinner":[{"19:00:00":"7:00 pm"},{"19:30:00":"7:30 pm"}]};

// Put this code inside of the "done callback"
var $elements = $('#breakfast, #lunch, #dinner');
$.each(data, function (dataKey, dataVal) {
    $elements.each(function(domElKey, domElVal){
        if (dataKey === domElVal.id) {
            $.each(dataVal, function(timeKey,timeVal){
                $.each(timeVal,function(timePropKey, timePropVal){
                $(domElVal).append("<option>"+timePropVal+"</option>");
                });
            })
       }
    });
}); 

A fiddle that contains the answer

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery ajax populate dropdown with json response data

From Dev

How to populate dropdownlist with JSON data as ajax response in jQuery

From Dev

populate dropdown with json response data for currency

From Dev

Parse json and populate dropdown by jquery

From Dev

Jquery 3.3.1 populate Dropdown from JSON object

From Dev

Populate dropdown with data from database using jQuery

From Dev

PHP jQuery Data-Attribute populate dropdown

From Dev

Populate dropdown dynamically using JSON data

From Dev

How to populate JSON response values in Jquery

From Dev

How to show json response data in datatable using jQuery ajax request?

From Dev

Display data in a dropdown with ajax json

From Dev

handle json response in jquery ajax

From Dev

Use Jquery ajax json response?

From Dev

Jquery/Ajax - Parse JSON Response

From Dev

Populate HTML table with JQuery AJAX using JSON formatted data: How to access data from variable in loop?

From Dev

Populate a second dropdown accoriding to the values returned by Jquery Ajax

From Dev

Populate JQuery Flot Chart with Ajax and Json

From Dev

dropdown menu populate another c# mvc json ajax

From Dev

dropdown menu populate another c# mvc json ajax

From Dev

Select2 - Ajax Data - Populate The Dropdown based on query

From Dev

Populate Highcharts series with data from AJAX/JSON

From Java

Populate a dropdown using jQuery

From Dev

Populate Dropdown List on page (not in dialog) from JSON data

From Dev

populate multiple dropdown select boxes from JSON data in angular

From Dev

Populate HTML5 dropdown values with JSON data

From Dev

jquery dropdown problem after ajax call data

From Dev

JQuery Autocomplete, populate with data from pHp json

From Dev

jquery to populate ul and li, based on JSON data

From Dev

bind html table data based on dropdown seletion using angularjs,jquery,json,ajax

Related Related

  1. 1

    Jquery ajax populate dropdown with json response data

  2. 2

    How to populate dropdownlist with JSON data as ajax response in jQuery

  3. 3

    populate dropdown with json response data for currency

  4. 4

    Parse json and populate dropdown by jquery

  5. 5

    Jquery 3.3.1 populate Dropdown from JSON object

  6. 6

    Populate dropdown with data from database using jQuery

  7. 7

    PHP jQuery Data-Attribute populate dropdown

  8. 8

    Populate dropdown dynamically using JSON data

  9. 9

    How to populate JSON response values in Jquery

  10. 10

    How to show json response data in datatable using jQuery ajax request?

  11. 11

    Display data in a dropdown with ajax json

  12. 12

    handle json response in jquery ajax

  13. 13

    Use Jquery ajax json response?

  14. 14

    Jquery/Ajax - Parse JSON Response

  15. 15

    Populate HTML table with JQuery AJAX using JSON formatted data: How to access data from variable in loop?

  16. 16

    Populate a second dropdown accoriding to the values returned by Jquery Ajax

  17. 17

    Populate JQuery Flot Chart with Ajax and Json

  18. 18

    dropdown menu populate another c# mvc json ajax

  19. 19

    dropdown menu populate another c# mvc json ajax

  20. 20

    Select2 - Ajax Data - Populate The Dropdown based on query

  21. 21

    Populate Highcharts series with data from AJAX/JSON

  22. 22

    Populate a dropdown using jQuery

  23. 23

    Populate Dropdown List on page (not in dialog) from JSON data

  24. 24

    populate multiple dropdown select boxes from JSON data in angular

  25. 25

    Populate HTML5 dropdown values with JSON data

  26. 26

    jquery dropdown problem after ajax call data

  27. 27

    JQuery Autocomplete, populate with data from pHp json

  28. 28

    jquery to populate ul and li, based on JSON data

  29. 29

    bind html table data based on dropdown seletion using angularjs,jquery,json,ajax

HotTag

Archive