PHP drop down list using array's and foreach (else and for) code



PHP Snippet 1:

<script src="path/to/jquery.js"></script>
<script>

var stateMap = {
    "USA": [ { "AL" : "AL"} .....],
    "CAN": [ { "AB" : "AB" }, .... ]
};

function setStates(country) {
    var newOptions = stateMap[country]; 
    var states = $('#states');
    states.empty();
    $.each(newOptions, function(key, value) {
        $el.append($("<option></option>")
        .attr("value", value).text(key));
        });
    }
}

// initialise on document load...
$(function() {
  setStates( "USA");
});

// set state list on country change....
$('#country').change( function() {
    var country = $( this ).val();

    setStates( country);

}
</script>
<body>
     <select id="country">
         <option value="USA">USA</option>
         <option value="CAN">Canada</option>
     </select>
     <select id="states">
     </select>
</body>