<?php
// Set delay 1 second.
sleep(1);
// Create connection connect to mysql database
$dbCon = mysql_connect('localhost', 'root', '1234') or die (mysql_error());
// Select database.
mysql_select_db('stock', $dbCon) or die (mysql_error());
// Set encoding.
mysql_query('SET NAMES UTF8');
// Next dropdown list.
$nextList = isset($_GET['nextList']) ? $_GET['nextList'] : '';
switch($nextList) {
case 'kindtype':
$kindid = isset($_GET['kindid']) ? $_GET['kindid'] : '';
$result = mysql_query("
SELECT
kind_type_id,
kind_type_name
FROM
stock_kind_type
WHERE kind_id = '{$kindid}'
ORDER BY CONVERT(kind_type_name USING TIS620) ASC;
");
break;
}
$data = array();
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
// Print the JSON representation of a value
echo json_encode($data);
?>
Code (PHP)
<script type="text/javascript" src="jquery.min.js"></script>
<?php
// Load jQuery library from google.
// Create connection connect to mysql database
$dbCon = mysql_connect('localhost', 'root', '1234') or die (mysql_error());
// Select database.
mysql_select_db('stock', $dbCon) or die (mysql_error());
// Set encoding.
mysql_query('SET NAMES UTF8');
?>
<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>';
var loadingImage = '<img src="images/loading4.gif" alt="loading" />';
// Bind an event handler to the "change" JavaScript event, or trigger that event on an element.
$('#selKind').change(function() {
$("#selKindtype").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: ({ nextList : 'kindtype', kindid: $('#selKind').val() }),
// The type of data that you're expecting back from the server.
dataType: "json",
// beforeSend is called before the request is sent
beforeSend: function() {
$("#waitKindtype").html(loadingImage);
},
// success is called if the request succeeds.
success: function(json){
$("#waitKindtype").html("");
// Iterate over a jQuery object, executing a function for each matched element.
$.each(json, function(index, value) {
// Insert content, specified by the parameter, to the end of each element
// in the set of matched elements.
$("#selKindtype").append('<option value="' + value.kind_type_id +
'">' + value.kind_type_name + '</option>');
});
}
});
});
</script>
<style type="text/css">
body {
font-family: Verdana, Geneva, sans-serif;
font-size: 13px;
}
</style>
</head>
<body>
<label>ประเภทอาวุธ : </label>
<select id="selKind">
<option value=""> ------- เลือก ------ </option>
<?php
$result = mysql_query("
SELECT
kind_id,
kind_name
FROM
stock_kind
ORDER BY CONVERT(kind_name USING TIS620) ASC;
");
while($row = mysql_fetch_assoc($result)){
echo '<option value="', $row['kind_id'], '">', $row['kind_name'],'</option>';
}
?>
</select>
<label>ชนิดอาวุธ : </label>
<select id="selKindtype">
<option value=""> ------- เลือก ------ </option>
</select><span id="waitKindtype"></span>
</body>
</html>