|
|
|
เอา Dropdown list Ajax+php+mysql & php+SqlServer2008+ajax+sql_srv function มาแบ่งกันใช้ เป็นอีกแนวทาง ใช้งานง่ายมาก |
|
|
|
|
|
|
|
Code (PHP)
Dropdown list Ajax php เอามาแบ่งกันใช้ เป็นอีกแนวทาง
Database ก็โหลดนี่เลย
https://www.thaicreate.com/free-web-script/thailand-province-database.html
index.php
<h1>Mysql php</h1>
<?php
echo "<form name=sel>";
echo "จังหวัด : <font id=province><select onkeypress='return handleEnter(this, event)' >";
echo "<option value='0'>--------------------</option>" ;
echo "</select></font><br><br><br>";
echo "อำเภอ : <font id=amper><select onkeypress='return handleEnter(this, event)' >";
echo "<option value='0'>--------------------</option> " ;
echo "</select></font><br><br><br>";
echo "ตำบล : <font id=tumbon><select onkeypress='return handleEnter(this, event)' >";
echo "<option value='0' diable>--------------------</option> " ;
echo "</select></font>";
?>
<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //รับค่ากลับมา
}
}
};
req.open("GET", "mysql_list_location.php?data="+src+"&val="+val); //สร้าง connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
req.send(null); //ส่งค่า
}
window.onLoad=dochange('province', -1);
</script>
mysql_list_location.php
Code (PHP)
<?php
//กำหนดให้ IE อ่าน page นี้ทุกครั้ง ไม่ไปเอาจาก cache lklkk
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header("content-type: application/x-javascript; charset=utf-8");
$data=$_GET['data'];
$val=$_GET['val'];
//ค่ากำหนดของ ฐานข้อมูล
$objConnect = mysql_connect("localhost","root","123") or die("Error Connect to Database");
$objDB = mysql_select_db("thailand");
if ($data=='province') {
echo "<select name='province' onChange=\"dochange('amper', this.value)\" onkeypress='return handleEnter(this, event)' >";
echo "<option value='' >เลือก</option> " ;
$strSQL = "SELECT * FROM `province` order by PROVINCE_NAME ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery)){
echo "<option value='$objResult[PROVINCE_ID]' >$objResult[PROVINCE_NAME]</option> " ;
}
echo "</select>\n";
} else if ($data=='amper') {
echo "<select name='amper' onChange=\"dochange('tumbon', this.value)\" onkeypress='return handleEnter(this, event)' >";
echo "<option value='' >เลือก</option> " ;
$strSQL = " SELECT AMPHUR_ID, AMPHUR_CODE, AMPHUR_NAME, GEO_ID, PROVINCE_ID
FROM amphur
WHERE (PROVINCE_ID ='$val')
ORDER BY AMPHUR_NAME ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery)){
echo "<option value='$objResult[AMPHUR_ID]' >$objResult[AMPHUR_NAME]</option> " ;
}
echo "</select>";
} else if ($data=='tumbon') {
echo "<select name='tumbon' onChange=\"dochange('qlocate', this.value)\" onkeypress='return handleEnter(this, event)' >";
echo "<option value='' >เลือก</option> " ;
$strSQL = " SELECT DISTRICT_ID, DISTRICT_CODE, DISTRICT_NAME, AMPHUR_ID, PROVINCE_ID, GEO_ID
FROM district
WHERE (AMPHUR_ID ='$val')
ORDER BY DISTRICT_NAME ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery)){
echo "<option value='$objResult[DISTRICT_ID]' >$objResult[DISTRICT_NAME]</option> " ;
}
echo "</select>\n";
}
?>
Tag : PHP, MySQL, Ms SQL Server 2008, JavaScript, Ajax
|
ประวัติการแก้ไข 2012-08-29 12:17:44 2012-08-29 12:25:31
|
|
|
|
|
Date :
2012-08-29 12:14:53 |
By :
tongkamlekdee |
View :
2971 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
อันนี้ Dropdownlist php+Sql server 2008 +ajax+ sql_srv
index.php
<?php
header("Content-Type: text/html; charset=tis-620");
echo "<form name=sel>";
echo "จังหวัด : <font id=province><select onkeypress='return handleEnter(this, event)' >";
echo "<option value='0'>--------------------</option>" ;
echo "</select></font><br><br><br>";
echo "อำเภอ : <font id=amper><select onkeypress='return handleEnter(this, event)' >";
echo "<option value='0'>--------------------</option> " ;
echo "</select></font><br><br><br>";
echo "ตำบล : <font id=tumbon><select onkeypress='return handleEnter(this, event)' >";
echo "<option value='0' diable>--------------------</option> " ;
echo "</select></font>";
?>
<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //รับค่ากลับมา
}
}
};
req.open("GET", "sqlsrv_list_location.php?data="+src+"&val="+val); //สร้าง connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
req.send(null); //ส่งค่า
}
window.onLoad=dochange('province', -1);
</script>
Code (PHP)
sqlsrv_list_location.php
<?php
//กำหนดให้ IE อ่าน page นี้ทุกครั้ง ไม่ไปเอาจาก cache lklkk
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header("content-type: application/x-javascript; charset=tis-620");
$data=$_GET['data'];
$val=$_GET['val'];
//ค่ากำหนดของ ฐานข้อมูล
$serverName = "localhost";
$connectionInfo = array( "Database"=>"thailand", "UID"=>"sa", "PWD"=>"abc+123");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
if ($data=='province') {
echo "<select name='province' onChange=\"dochange('amper', this.value)\" onkeypress='return handleEnter(this, event)' >";
$sql = "SELECT PROVINCE_ID, PROVINCE_CODE, PROVINCE_NAME, GEO_ID
FROM province
ORDER BY PROVINCE_NAME";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) { die( print_r( sqlsrv_errors(), true));}
echo "<option value='' >เลือก</option> " ;
while( $obj = sqlsrv_fetch_object( $stmt)) {
echo "<option value='$obj->PROVINCE_ID' >$obj->PROVINCE_NAME</option> " ;
}
echo "</select>\n";
} else if ($data=='amper') {
echo "<select name='amper' onChange=\"dochange('tumbon', this.value)\" onkeypress='return handleEnter(this, event)' >";
$sql = " SELECT AMPHUR_ID, AMPHUR_CODE, AMPHUR_NAME, GEO_ID, PROVINCE_ID
FROM amphur
WHERE (PROVINCE_ID ='$val')
ORDER BY AMPHUR_NAME";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) { die( print_r( sqlsrv_errors(), true));}
echo "<option value='' >เลือก</option> " ;
while( $obj = sqlsrv_fetch_object( $stmt)) {
echo "<option value='$obj->AMPHUR_ID' >$obj->AMPHUR_NAME</option> " ;
}
echo "</select>";
} else if ($data=='tumbon') {
echo "<select name='tumbon' onChange=\"dochange('qlocate', this.value)\" onkeypress='return handleEnter(this, event)' >";
$sql = " SELECT DISTRICT_ID, DISTRICT_CODE, DISTRICT_NAME, AMPHUR_ID, PROVINCE_ID, GEO_ID
FROM district
WHERE (AMPHUR_ID ='$val')
ORDER BY DISTRICT_NAME";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) { die( print_r( sqlsrv_errors(), true));}
echo "<option value='' >เลือก</option> " ;
while( $obj = sqlsrv_fetch_object( $stmt)) {
echo "<option value='$obj->DISTRICT_ID' >$obj->DISTRICT_NAME</option> " ;
}
echo "</select>\n";
}
?>
|
ประวัติการแก้ไข 2012-08-29 12:23:29
|
|
|
|
Date :
2012-08-29 12:22:21 |
By :
tongkamlekdee |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แจ่มครับ
|
|
|
|
|
Date :
2012-08-29 12:51:38 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|