<?php
// Load jQuery library from google.
$jqLib = 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js';
$brand = array(
't1' => 'toyota',
'bm' => 'bmw',
'ho' => 'honda'
);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dependent dropdownlist จังหวัด อำเภอ ตำบล</title>
<script type="text/javascript" src="<?php echo $jqLib; ?>"></script>
<script type="text/javascript">
// Specify a function to execute when the DOM is fully loaded.
$(function(){
var defaultOption = '<option value=""> ------- เลือก ------ </option>';
// Bind an event handler to the "change" JavaScript event, or trigger that event on an element.
$('#selBrand').change(function() {
$("#selCar").html(defaultOption);
// Perform an asynchronous HTTP (Ajax) request.
$.ajax({
// A string containing the URL to which the request is sent.
url: "jsonAction.php",
// Data to be sent to the server.
data: ({ brand: $('#selBrand').val() }),
// The type of data that you're expecting back from the server.
dataType: "json",
// success is called if the request succeeds.
success: function(json){
// Iterate over a jQuery object, executing a function for each matched element.
$.each(json, function(index, value) {
console.debug(value);
// Insert content, specified by the parameter, to the end of each element
// in the set of matched elements.
$("#selCar").append('<option value="' + value +
'">' + value + '</option>');
});
}
});
});
});
</script>
<style type="text/css">
body {
font-family: Verdana, Geneva, sans-serif;
font-size: 13px;
}
</style>
</head>
<body>
<label>BRAND : </label>
<select id="selBrand">
<option value=""> ------- เลือก ------ </option>
<?php
foreach($brand as $k => $v){
echo '<option value="', $k, '">', $v,'</option>';
}
?>
</select>
<label>CAR : </label>
<select id="selCar">
<option value=""> ------- เลือก ------ </option>
</body>
</html>