Skip to content Skip to sidebar Skip to footer

Ajax Form, 2 Drop Downs With External Source

is there a way once I select from a drop down list on a form that it will select all models from the first dropdown in a second drop down Here is my code

Solution 1:

Here's some rough code on how to accomplish this.

On page load, populate dropdown1.

$(function(){
    $.get('file1.php', function(data){
       $('#dropdown1').html( data ); 
    });

    // on change of dropdown1 populate dropdown2 with the respective data 
    $('#dropdown1').change(function(){
        $.get('file2.php',{ make: $(this).val() }, function(data){
            $('#dropdown2').html( data ); 
        }); 
    }); 
}); 

So in your file2.php you can check the $_GET array for the make variable passed in, then you can send back all the options for that model. You could also send back json instead of html but this gives you a rough idea of how to do it and i think it's best to keep it simple at first.

Post a Comment for "Ajax Form, 2 Drop Downs With External Source"