<?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('thailand', $dbCon) or die (mysql_error());
// Set encoding.
mysql_query('SET NAMES UTF8');
// Next dropdown list.
$nextList = isset($_GET['nextList']) ? $_GET['nextList'] : '';
switch($nextList) {
case 'class':
$brand_id = isset($_GET['brand_id']) ? $_GET['brand_id'] : '';
$result = mysql_query("
SELECT
class_id,
class_name
FROM
class
WHERE brand_id = '{$brand_id}'
ORDER BY CONVERT(class_name USING TIS620) ASC;
");
break;
case 'type':
$class_id = isset($_GET['class_id']) ? $_GET['class_id'] : '';
$result = mysql_query("
SELECT
type_id,
type_name
FROM
district
WHERE class_id = '{$class_id}'
ORDER BY CONVERT(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);
?>
[head]Code (PHP)
<?php
// Load jQuery library from google.
$jqLib = 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js';
// Create connection connect to mysql database
$dbCon = mysql_connect('localhost', 'root', '1234') or die (mysql_error());
// Select database.
mysql_select_db('motor', $dbCon) or die (mysql_error());
// Set encoding.
mysql_query('SET NAMES UTF8');
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<? include"menu_admin.php";?>
<? include"connect.php";?>
<br /><br />
<script type="text/javascript" src="<?php echo $jqLib; ?>"></script>
<script type="text/javascript">
$(function(){
var defaultOption = '<option value=""> ------- เลือก ------ </option>';
var loadingImage = '<img src="image/loading4.gif" alt="loading" />';
// Bind an event handler to the "change" JavaScript event, or trigger that event on an element.
$('#selBrand').change(function() {
$("#selClass").html(defaultOption);
$("#selType").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 : 'class', brand_id: $('#selBrand').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() {
$("#waitClass").html(loadingImage);
},
// success is called if the request succeeds.
success: function(json){
$("#waitClass").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.
$("#selClass").append('<option value="' + value.class_id +
'">' + value.class_name + '</option>');
});
}
});
});
$('#selClass').change(function() {
$("#selType").html(defaultOption);
$.ajax({
url: "jsonAction.php",
data: ({ nextList : 'type', class_id: $('#selClass').val() }),
dataType: "json",
beforeSend: function() {
$("#waitType").html(loadingImage);
},
success: function(json){
$("#waitType").html("");
$.each(json, function(index, value) {
$("#selType").append('<option value="' + value.type_id +
'">' + value.type_name + '</option>');
});
}
});
});
});
</script>
<style type="text/css">
body {
font-family: Verdana, Geneva, sans-serif;
font-size: 13px;
}
</style>
<link href="main_style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.css">
<body background="image/bg.jpg">
<fieldset style="font-size:14px"><legend><b>รายละเอียดรถ</b></legend>
<form action="save_frm_store.php" method="post">
<table width="100%" height="40" align="left" style="font-size:13px">
<tr>
<td width="15%" align="right">เลขที่ใบรับสินค้า :</td>
<td width="22%" align="left"><input type="text" name="bill_number" id="bill_number" class="tbl_box"/></td>
<td width="15%" align="right">วันที่รับรถ :</td>
<td width="22%" align="left">
<input type="text" name="date_receive" id="date_receive" class="tbl_box1" size="10"/>
<input type="hidden" name="birth" value="<?=$date_receive; ?>" />
</td>
<td width="15%" align="right">รับรถจากสาขา :</td>
<td align="22%">สภาพรถ :</td>
<td>
<select name="branch">
<?
$strSQL = "SELECT * FROM branch ORDER BY branch_id ASC";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery))
{
?>
<option value="<?=$objResult["branch_id"];?>">
<?=$objResult["branch_no"];?>--<?=$objResult["branch_name"];?>
</option>
<?
}
?>
</select>
</td>
</tr>
<tr>
<td align="right">เลขเครื่อง :</td>
<td><input type="text" name="machine_id" id="machine_id" class="tbl_box1"/></td>
<td width="8%" align="right">เลขถัง :</td>
<td width="22%"><input type="text" name="chassis_id" id="chassis_id" class="tbl_box" /></td>
</tr>
<tr>
<td align="right">ผู้รับรถ :</td>
<td><input type="text" name="receive_name" id="receive_name" class="tbl_box"/></td>
</tr>
<tr>
<td height="25" align="right">ยี่ห้อรถ :</td>
<td height="25">
<select id="selBrand">
<option value=""> ------- เลือก ------ </option>
<?php
$result = mysql_query("
SELECT
brand_id,
brand_name
FROM
brand
ORDER BY CONVERT(brand_name USING TIS620) ASC;
");
while($row = mysql_fetch_assoc($result)){
echo '<option value="', $row['brand_id'], '">', $row['brand_name'],'</option>';
}
?>
</select>
</td>
<td width="10%" align="right">รุ่น :</td>
<td>
<select id="selclass">
<option value=""> ------- เลือก ------ </option>
</select><span id="waitClass"></span>
</td>
<td align="right">แบบ :</td>
<td>
<select id="seltype">
<option value=""> ------- เลือก ------ </option>
</select><span id="waitType"></span>
</td>
<td align="right">สี :</td>
<td><input type="text" name="color" id="color" /></td>
</tr>
<input type="hidden" id="brandHidden" name="brandHidden" />
<input type="hidden" id="classHidden" name="classHidden" />
<input type="hidden" id="typeHidden" name="typeHidden" />
<tr>
<td align="right">ขนาด (C.C.) :</td>
<td><input type="text" name="cc" id="cc" class="tbl_box"/></td>
<td align="right">ป้ายทะเบียน :</td>
<td><input type="text" name="plate" id="plate" class="tbl_box"/></td>
<td align="right">จังหวัด :</td>
<td>
<select name="province">
<?
$strSQL = "SELECT * FROM province ORDER BY province_id ASC";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery))
{
?>
<option value="<?=$objResult["province_id"];?>"><?=$objResult["province_name"];?></option>
<?
}
?>
</select>
</td>
</tr>
<tr>
<td align="right">ปีที่จดทะเบียน :</td>
<td><input type="text" name="regis_year" id="regis_year" class="tbl_box1"/></td>
<td align="right">สภาพรถ :</td>
<td>
<select name="status_motor">
<?
$strSQL = "SELECT * FROM status_motor ORDER BY stm_id ASC";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery))
{
?>
<option value="<?=$objResult["stm_id"];?>"><?=$objResult["stm_name"];?></option>
<?
}
?>
</select>
</td>
</tr>
<tr>
<td align="right">ราคาซื้อ :</td>
<td><input type="text" name="buy" id="buy" class="tbl_box"/></td>
<td align="right">ราคาขาย :</td>
<td><input type="text" name="sell" id="sell" /></td>
</tr>
<tr>
<td colspan="6" align="center"><input type="submit" name="Submit" value="บันทึก"></td>
</tr>
</table>
</form>
</fieldset>
<?
mysql_close($objConnect);
?>
</fieldset>