 |
|
พอดี ผมลองเอาโค้ด จาก เว็บ http://www.code-father.com/(%E0%B8%A0%E0%B8%B2%E0%B8%84%E0%B8%95%E0%B9%88%E0%B8%AD-%E0%B9%81%E0%B8%81%E0%B9%89%E0%B9%84%E0%B8%82-%E0%B8%AD%E0%B8%B1%E0%B8%9E%E0%B9%80%E0%B8%94%E0%B8%97)-%E0%B9%82%E0%B8%84%E0%B9%89%E0%B8%94%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%80%E0%B8%A5%E0%B8%B7%E0%B8%AD%E0%B8%81%E0%B8%88%E0%B8%B1%E0%B8%87%E0%B8%AB%E0%B8%A7%E0%B8%B1%E0%B8%94-%E0%B8%AD%E0%B8%B3%E0%B9%80%E0%B8%A0%E0%B8%AD-%E0%B8%95%E0%B8%B3%E0%B8%9A%E0%B8%A5-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-Ajax-Code-PHP-select-Provicne-Umphur-District-with-Edit_00042.html
แต่ผมลอง เล่น และ รัน ใน chome กะ ie มะยัก กะเล่นได้
code ไฟล์ index.php
Code
<?php
$host = "localhost";
$user_admin ="root";
$password = "12345";
$database = "province";
$link = mysql_connect($host,$user_admin,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
//mysql_query("SET CHARACTER SET tis620");
mysql_query("set character set utf8");
if($Save=="YES") echo $province.'<< province id ',$amphur.'<< amphur id ',$district.'<< district id ';
?>
<!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>Untitled Document</title>
</head>
<script language="JavaScript">
function Check_txt(){
if(document.getElementById('province').value==""){
alert("กรุณาระบุ จังหวัด ด้วยค่ะ");
document.getElementById('province').focus();
return false;
}
if(document.getElementById('amphur').value==''){
alert("กรุณาระบุ อำเภอ ด้วยค่ะ");
document.getElementById('amphur').focus();
return false;
}
if(document.getElementById('district').value==""){
alert("กรุณาระบุ ตำบล ด้วยค่ะ");
document.getElementById('district').focus();
return false;
}
}
</script>
<body>
<? if($action=='' or $action=="Add"){?>
<!-- Add New --> Add New
<form name="frm1" action="./?Save=YES&action=Edit" method="post" onSubmit="return Check_txt()" enctype='multipart/form-data'>
จังหวัด :
<select name='province' id='province' onchange="data_show(this.value,'amphur','');">
<option value="">--- Pleaser Select Province ---</option>
<?
$rstTemp=mysql_query('select * from province Order By PROVINCE_ID ASC');
while($arr_2=mysql_fetch_array($rstTemp)){
?>
<option value="<?=$arr_2['PROVINCE_ID']?>"><?=$arr_2['PROVINCE_NAME']?></option>
<? }?>
</select>
<br />อำเภอ :
<select name='amphur' id='amphur'onchange="data_show(this.value,'district','');">
<option value="">--- Pleaser Select Province ---</option>
</select>
<br />ตำบล :
<select name='district' id='district'>
<option value="">--- Pleaser Select Amphur ---</option>
</select>
<br /><input type="submit" name="Submit" value="Submit" />
</form>
<!-- Edit -->Edit
<form name="frm1" action="./?Save=YES&action=Edit" method="post" onSubmit="return Check_txt()" enctype='multipart/form-data'>
จังหวัด :
<select name='province' id='province' onchange="data_show(this.value,'amphur','');document.getElementById('district').innerHTML = '<option value=>--- Pleaser Select Amphur ---</option>';">
<option value="">--- Pleaser Select Province ---</option>
<?
$rstTemp=mysql_query('select * from province Order By PROVINCE_ID ASC');
while($arr_2=mysql_fetch_array($rstTemp)){
?>
<option value="<?=$arr_2['PROVINCE_ID']?>" <? if($arr_2['PROVINCE_ID']==$province) echo "selected";?>><?=$arr_2['PROVINCE_NAME']?></option>
<? }?>
</select>
<br />อำเภอ :
<select name='amphur' id='amphur' onchange="data_show(this.value,'district','');">
<option value="">--- Pleaser Select Province ---</option>
</select>
<br />ตำบล :
<select name='district' id='district'>
<option value="">--- Pleaser Select Amphur ---</option>
</select>
<br /><input type="submit" name="Submit" value="Submit" />
</form>
<!-- Edit -->
<? }?>
</body>
<script language="javascript">
// Start XmlHttp Object
function uzXmlHttp(){
var xmlhttp = false;
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlhttp = false;
}
}
if(!xmlhttp && document.createElement){
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
// End XmlHttp Object
function data_show(select_id,result,point_id){
var url = 'data.php?select_id='+select_id+'&result='+result+'&point_id='+point_id;
//alert(url);
xmlhttp = uzXmlHttp();
xmlhttp.open("GET", url, false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
xmlhttp.send(null);
document.getElementById(result).innerHTML = xmlhttp.responseText;
}
if('<?=$action?>'=='Edit'){
window.onLoad=data_show('<?=$province?>','amphur','<?=$amphur?>');
window.onLoad=data_show('<?=$amphur?>','district','<?=$district?>');
}
</script>
</html>
ไฟล์ data.php
Code
<?
$host = "localhost";
$user_admin ="root";
$password = "12345";
$database = "province";
$link = mysql_connect($host,$user_admin,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
//mysql_query("SET CHARACTER SET tis620");
mysql_query("set character set utf8");
?>
<? if($result=='amphur'){
$rstTemp=mysql_query("select * from amphur Where PROVINCE_ID ='".$select_id."' Order By AMPHUR_ID ASC");
while($arr_2=mysql_fetch_array($rstTemp)){
?>
<option value="<?=$arr_2['AMPHUR_ID']?>" <? if($arr_2['AMPHUR_ID']==$point_id) echo "selected";?>><?=$arr_2['AMPHUR_NAME']?></option>
<? }}?>
<? if($result=='district'){ ?>
<select name='district' id='district'>
<?
$rstTemp=mysql_query("select * from district Where AMPHUR_ID ='".$select_id."' Order By DISTRICT_ID ASC");
while($arr_2=mysql_fetch_array($rstTemp)){
?>
<option value="<?=$arr_2['DISTRICT_ID']?>" <? if($arr_2['DISTRICT_ID']==$point_id) echo "selected";?>><?=$arr_2['DISTRICT_NAME']?></option>
<? }?>
</select>
<? }?>
ฐานข้อมูล

ต้องการนำ มาใช้อะคับ ควร แก้ ตรงไหน หรือ เพิ่ม อะไร บ้าง
Tag : PHP, MySQL, JavaScript, Ajax, jQuery
|
|
 |
 |
 |
 |
Date :
2013-08-15 10:47:05 |
By :
komza |
View :
1508 |
Reply :
7 |
|
 |
 |
 |
 |
|
|
|
 |