0

I have a dropdown that lists the data fetched from database using PHP and jQuery Ajax.

  1. First dropdown List fetches data successfully and populates it in the dropdown.

HTML for first dropdownlist :

$(document).ready(function () {         
        $(function() {   
            $.ajax({
                type: 'POST',
                url: 'getGroupzBase.php', 
                datatype: 'json',
                success: function(data) {
                // Call this function on success                
                    console.log(data);
                   var yourArray = JSON.parse(data);
                   console.log(yourArray);                                           
                    $.each(yourArray, function (index, yourArray) {                 
                        $('#builder_group').append($('<option/>', { 
                            value: yourArray.id,
                            text : yourArray.name, 
                        }));
}); 
},
error: function() {
    displayDialogBox('Error', err.toString());
}
});              
});

And the dropdown tag -

<select id="builder_group"></select>
  1. For the second dropdown menu does not work with the first dropdown. I have to use jQuery Ajax for the second dropdown as well.
    HTML for second dropdownlist :

      <script>
        $("#builder_group").change(function(){
            console.log("Hello 1");
            $('#Ivrmapping_groupZCode').find('option').remove().end(); //clear the city ddl
            var builder = $(this).find("option:selected").text();
            alert(builder);
            //do the ajax call
            $.ajax({
                url:'getGroupzCode.php'
                type:'GET',
                data:{city:builder},
                dataType:'json',
                cache:false,
            success: function(data) {
                        // Call this function on success                
                            console.log(data);
                           var yourArray = JSON.parse(data);
                           console.log(yourArray);                                           
                            $.each(yourArray, function (index, yourArray) {                 
                            );
        }); 
        },
        error: function() {
            displayDialogBox('Error', err.toString());
        }
        }); 
    
            }); 
    
        });
    </script>
    

And the dropdown tag -

<select name="Ivrmapping[groupZCode]" id="Ivrmapping_groupZCode">..
</select>

Why is the second dropdownlist not working with first. Can we have more than one jQuery Ajax calls in one page.

7
  • Yes definitely we can have them.. is your console.log(data) giving you fine results(as you expect).. Commented Jul 16, 2014 at 12:08
  • @nsthethunderbolt No I don't see any of those console.log in second jQuery ajax script Commented Jul 16, 2014 at 12:12
  • I think you are passing in value... shouldn't you be passing id value in city:builder Commented Jul 16, 2014 at 12:13
  • @nsthethunderbolt Still if I place alert before that atleast that should work right Commented Jul 16, 2014 at 12:14
  • Yes that should work, are you sure you are handling the value (of builder) at back end rather than the id.. might be a silly mistake. I don't see any other problem, Have you tried to look at the response of the ajax call in console of browser. Commented Jul 16, 2014 at 12:18

1 Answer 1

1

I think there is problem of brackets:: Try this:

$("#builder_group").change(function(){
        console.log("Hello 1");
        $('#Ivrmapping_groupZCode').find('option').remove().end(); //clear the city ddl
        var builder = $(this).find("option:selected").text();
        alert(builder);
        //do the ajax call
        $.ajax({
            url:'getGroupzCode.php'
            type:'GET',
            data:{city:builder},
            dataType:'json',
            cache:false,
        success: function(data) {
                    // Call this function on success                
                        console.log(data);
                       var yourArray = JSON.parse(data);
                       console.log(yourArray);                                           
                        $.each(yourArray, function (index, yourArray) {/* your plan*/});
    },
    error: function() {
        displayDialogBox('Error', err.toString());
    }
    }); 
    });

And you should use firebug, or chrome, these errors are clearly shown on the console. Try it out.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.