<?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('thailand', $dbCon) or die (mysql_error());
// Set encoding.
mysql_query('SET NAMES UTF8');
?>
<!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>';
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.
$('#selProvince').change(function() {
$("#selAmphur").html(defaultOption);
$("#selTumbon").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 : 'amphur', provinceID: $('#selProvince').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() {
$("#waitAmphur").html(loadingImage);
},
// success is called if the request succeeds.
success: function(json){
$("#waitAmphur").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.
$("#selAmphur").append('<option value="' + value.AMPHUR_ID +
'">' + value.AMPHUR_NAME + '</option>');
});
}
});
});
$('#selAmphur').change(function() {
$("#selTumbon").html(defaultOption);
$.ajax({
url: "jsonAction.php",
data: ({ nextList : 'tumbon', amphurID: $('#selAmphur').val() }),
dataType: "json",
beforeSend: function() {
$("#waitTumbon").html(loadingImage);
},
success: function(json){
$("#waitTumbon").html("");
$.each(json, function(index, value) {
$("#selTumbon").append('<option value="' + value.DISTRICT_ID +
'">' + value.DISTRICT_NAME + '</option>');
});
}
});
});
});
</script>
<style type="text/css">
body {
font-family: Verdana, Geneva, sans-serif;
font-size: 13px;
}
</style>
</head>
<body>
<label>จังหวัด : </label>
<select id="selProvince">
<option value=""> ------- เลือก ------ </option>
<?php
$result = mysql_query("
SELECT
PROVINCE_ID,
PROVINCE_NAME
FROM
province
ORDER BY CONVERT(PROVINCE_NAME USING TIS620) ASC;
");
while($row = mysql_fetch_assoc($result)){
echo '<option value="', $row['PROVINCE_ID'], '">', $row['PROVINCE_NAME'],'</option>';
}
?>
</select>
<label>อำเภอ : </label>
<select id="selAmphur">
<option value=""> ------- เลือก ------ </option>
</select><span id="waitAmphur"></span>
<label>ตำบล : </label>
<select id="selTumbon">
<option value=""> ------- เลือก ------ </option>
</select><span id="waitTumbon"></span>
</body>
</html>
jsonAction.php
<?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 'amphur':
$provinceID = isset($_GET['provinceID']) ? $_GET['provinceID'] : '';
$result = mysql_query("
SELECT
AMPHUR_ID,
AMPHUR_NAME
FROM
amphur
WHERE PROVINCE_ID = '{$provinceID}'
ORDER BY CONVERT(AMPHUR_NAME USING TIS620) ASC;
");
break;
case 'tumbon':
$amphurID = isset($_GET['amphurID']) ? $_GET['amphurID'] : '';
$result = mysql_query("
SELECT
DISTRICT_ID,
DISTRICT_NAME
FROM
district
WHERE AMPHUR_ID = '{$amphurID}'
ORDER BY CONVERT(DISTRICT_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);
?>
ผมลองใส่ Function เข้าไป มันไม่รันให้ครับ ผมว่ามันต้องรันแต่ไม่รู้ว่า ผมเอาไปวางผิดตรงไหนหรือป่าว Function ที่ใช้นะครับ คือ Code (JavaScript)
<script type="text/javascript">
var objRequest = createRequestObject();
function createRequestObject() {
var objTemp = false;
if (window.XMLHttpRequest) {
objTemp = new XMLHttpRequest();
} else {
objTemp = new ActiveXObject("Microsoft.XMLHTTP");
}
return objTemp;
}
function getData(area1,area2,url1,url2,cateid,pbrand,pmaterial,ptype,set)
{
if (objRequest) {
var idst=cateid;
var rnd=Math.random();
var sendid="";
sendid+=url2+"rnd="+rnd+"&catid="+idst+"&brandid="+pbrand+"&materialid="+pmaterial+"&typeid="+ptype;
objRequest.open("GET",sendid);
objRequest.onreadystatechange=function handleResponse()
{
var objDiv = document.getElementById(area2);
if (objRequest.readyState==4 && objRequest.status==200)
{
objDiv.innerHTML=objRequest.responseText;
}
}
objRequest.send(null);
}
}
<?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('db522021213', $dbCon) or die (mysql_error());
// Set encoding.
mysql_query('SET NAMES UTF8');
?>
<!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>';
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.
$('#selRegion').change(function() {
$("#selProvince").html(defaultOption);
// Perform an asynchronous HTTP (Ajax) request.
$.ajax({
// A string containing the URL to which the request is sent.
url: "jsonAction2.php",
// Data to be sent to the server.
data: ({ nextList : 'province', RID: $('#selRegion').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() {
$("#waitProvince").html(loadingImage);
},
// success is called if the request succeeds.
success: function(json){
$("#waitProvince").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.
$("#selProvince").append('<option value="' + value.P_ID +
'">' + value.P_Name + '</option>');
});
}
});
});
});
</script>
<style type="text/css">
body {
font-family: Verdana, Geneva, sans-serif;
font-size: 13px;
}
</style>
</head>
<body>
<label>ภาค: </label>
<select id="selRegion">
<option value=""> ------- เลือก ------ </option>
<? $sql2="select * from region";
$result = mysql_query($sql2);
while($content = mysql_fetch_array($result)){
$content2=$content[R_ID];
$content3=$content[R_Name];
?>
<option value="<?php echo "$content2"; ?>"><?php echo "$content3"; ?></option>
<?php }?>
</select>
<label>จังหวัด: </label>
<select id="selProvince">
<option value=""> ------- เลือก ------ </option>
</select><span id ="waitProvince"></span>
</body>
</html>
jsonAction2.php
<?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('db522021213', $dbCon) or die (mysql_error());
// Set encoding.
mysql_query('SET NAMES UTF8');
// Next dropdown list.
$nextList = isset($_GET['nextList']) ? $_GET['nextList'] : '';
switch($nextList) {
case 'province':
$RID = isset($_GET['RID']) ? $_GET['RID'] : '';
$result = mysql_query("SELECT P_ID, P_Name FROM province WHERE R_ID = '{$RID}';
");
break;
}
$data = array();
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
// Print the JSON representation of a value
echo json_encode($data);
?>
<?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('db522021213', $dbCon) or die (mysql_error());
// Set encoding.
mysql_query('SET NAMES UTF8');
?>
<!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>';
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.
$('#selRegion').change(function() {
$("#selProvince").html(defaultOption);
// Perform an asynchronous HTTP (Ajax) request.
$.ajax({
// A string containing the URL to which the request is sent.
url: "jsonAction2.php",
// Data to be sent to the server.
data: ({ nextList : 'province', RID: $('#selRegion').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() {
$("#waitProvince").html(loadingImage);
},
// success is called if the request succeeds.
success: function(json){
$("#waitProvince").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.
$("#selProvince").append('<option value="' + value.P_ID +
'">' + value.P_Name + '</option>');
});
}
});
});
});
</script>
<style type="text/css">
body {
font-family: Verdana, Geneva, sans-serif;
font-size: 13px;
}
</style>
</head>
<body>
<label>ภาค: </label>
<select id="selRegion">
<option value=""> ------- เลือก ------ </option>
<? $sql2="select * from region";
$result = mysql_query($sql2);
while($content = mysql_fetch_array($result)){
$content2=$content[R_ID];
$content3=$content[R_Name];
?>
<option value="<?php echo "$content2"; ?>"><?php echo "$content3"; ?></option>
<?php }?>
</select>
<label>จังหวัด: </label>
<select id="selProvince">
<option value=""> ------- เลือก ------ </option>
</select><span id ="waitProvince"></span>
</body>
</html>
jsonAction2.php
<?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('db522021213', $dbCon) or die (mysql_error());
// Set encoding.
mysql_query('SET NAMES UTF8');
// Next dropdown list.
$nextList = isset($_GET['nextList']) ? $_GET['nextList'] : '';
switch($nextList) {
case 'province':
$RID = isset($_GET['RID']) ? $_GET['RID'] : '';
$result = mysql_query("SELECT P_ID, P_Name FROM province WHERE R_ID = '{$RID}';
");
break;
}
$data = array();
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
// Print the JSON representation of a value
echo json_encode($data);
?>
พี่ครับ พอผมเอาไปรันแล้วมันขึ้นแบบนี้อะครับ
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in D:\xampp\htdocs\drop_old1\thailand1\thailand\index.php:6 Stack trace: #0 {main} thrown in D:\xampp\htdocs\drop_old1\thailand1\thailand\index.php on line 6