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

Ashif Shereef

I scrapped this code to check if the mechanism works. Actually what I needed to achieve was to populate a "city dropdown" as per the selection made with the "state" dropdown. The HTML code is as follows.

     <select id="id"></select>
     <select id="state"></select>
     <select id="city"></select>

The Ajax call is as follows.

$(document).on("change", '#state', function(e) {
            var state = $(this).val();


        $.ajax({
            type: "POST",
            data: {state: state},
            url: 'get_name_list.php',
            dataType: 'json',
            success: function(json) {

                var $el = $("#city");
                $el.empty(); // remove old options
                $el.append($("<option></option>")
                        .attr("value", '').text('Please Select'));
                $.each(json, function(value, key) {
                    $el.append($("<option></option>")
                            .attr("value", value).text(key));
                });                                                     




            }
        });

    });

The PHP code encodes the list of values into a JSON with the following structure.

As a test, I inserted the JSON into a Database table and it is working until this point. So it is not a theoretical process. It works until this point in the code.

The JSON structure is as follows.

[{"name":"Alappuzha"},{"name":"Ernakulam"},{"name":"Idukki"},{"name":"Kannur"},{"name":"Kasaragod"},{"name":"Kollam"},{"name":"Kottayam"},{"name":"Kozhikode"},{"name":"Malappuram"},{"name":"Palakkad"},{"name":"Pathanamthitta"},{"name":"Thiruvananthapuram"},{"name":"Thrissur"},{"name":"Wayanad"}]

Then I echo the JSON.

But the dropdown back at the first page is not getting populated. The PHP file is as follows.

$json= array();
$id=$_POST["id"];
//echo  $sample;

 //mysqli_query($mysqli,"INSERT into test_tablr values('','$id')") or die(mysqli_error($mysqli)); 


if ($result = $mysqli->query("SELECT name FROM geo_locations  WHERE parent_id =$id ")) {

    while ($row = mysqli_fetch_assoc($result)) {
            $json1[] = $row;
    }
    $json=json_encode($json1);
}


mysqli_query($mysqli,"INSERT into test_tablr values('','$json')") or die(mysqli_error($mysqli)); 
echo $json;

But unfortunately, the dropdown is not getting populated. Any lights?

Ishan Shah

Try This in jQuery:

    var $el = $("#city"); 
    $el.empty(); // remove old options
    $el.append($("<option value=''>Please Select</option>");
    $.each(json, function(value, key) {
        $el.append($("<option value='"+ value +"'>"+key+"</option>");
    });           

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

Jquery ajax populate dropdown with json response data

From Dev

How do I fill a dropdown with values returned by servlet using AJAX

From Dev

populate second dropdown on onchange event

From Java

Populate a dropdown using jQuery

From Dev

Populate HTML table based on second dropdown selection

From Dev

Parse json and populate dropdown by jquery

From Dev

populate dropdown using codeigniter jquery

From Dev

Use dropdown list selection to populate second dropdown list contents

From Dev

Ruby - Populate and Array with returned method values

From Dev

Ruby - Populate and Array with returned method values

From Dev

Populate dropdown using ajax in Dancer framework

From Dev

Unable to populate chained dropdown list with Ajax and Javascript

From Dev

Polymer dropdown menu -- populate via ajax?

From Dev

populate dropdown via ajax and automatically select a value

From Dev

Populate dropdown using ajax in Dancer framework

From Dev

dropdown menu with jquery second level

From Dev

AngularJS - Use data returned by a service in the controller and the view to populate dropdown

From Dev

rails 4 populate dropdown values from database

From Dev

Populate Dropdown with Values from ActiveRecord query

From Dev

jquery ajax call returned data

From Dev

jQuery not work with Ajax returned code

From Dev

undefined returned in alertbox jquery ajax

From Dev

Populate dropdown with data from database using jQuery

From Dev

Jquery - Populate DropDown box with contents of array

From Dev

Jquery populate textarea from dropdown list

From Dev

jquery - How to populate dropdown/select/combobox dynamically?

From Dev

PHP jQuery Data-Attribute populate dropdown

From Dev

Jquery populate textarea from dropdown list

Related Related

  1. 1

    Jquery ajax populate dropdown with json response data

  2. 2

    Jquery ajax populate dropdown with json response data

  3. 3

    How do I fill a dropdown with values returned by servlet using AJAX

  4. 4

    populate second dropdown on onchange event

  5. 5

    Populate a dropdown using jQuery

  6. 6

    Populate HTML table based on second dropdown selection

  7. 7

    Parse json and populate dropdown by jquery

  8. 8

    populate dropdown using codeigniter jquery

  9. 9

    Use dropdown list selection to populate second dropdown list contents

  10. 10

    Ruby - Populate and Array with returned method values

  11. 11

    Ruby - Populate and Array with returned method values

  12. 12

    Populate dropdown using ajax in Dancer framework

  13. 13

    Unable to populate chained dropdown list with Ajax and Javascript

  14. 14

    Polymer dropdown menu -- populate via ajax?

  15. 15

    populate dropdown via ajax and automatically select a value

  16. 16

    Populate dropdown using ajax in Dancer framework

  17. 17

    dropdown menu with jquery second level

  18. 18

    AngularJS - Use data returned by a service in the controller and the view to populate dropdown

  19. 19

    rails 4 populate dropdown values from database

  20. 20

    Populate Dropdown with Values from ActiveRecord query

  21. 21

    jquery ajax call returned data

  22. 22

    jQuery not work with Ajax returned code

  23. 23

    undefined returned in alertbox jquery ajax

  24. 24

    Populate dropdown with data from database using jQuery

  25. 25

    Jquery - Populate DropDown box with contents of array

  26. 26

    Jquery populate textarea from dropdown list

  27. 27

    jquery - How to populate dropdown/select/combobox dynamically?

  28. 28

    PHP jQuery Data-Attribute populate dropdown

  29. 29

    Jquery populate textarea from dropdown list

HotTag

Archive