|
 |
|
ajax เลือกจังหวัด อำเภอ ตำบล ไม่ยอมส่งค่าkey ที่เรา select ใน chrome |
|
 |
|
|
 |
 |
|
ดูโค้ดอย่างเดียวครับ งานนี้
|
 |
 |
 |
 |
Date :
2011-12-23 16:19:02 |
By :
kalamell |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โค๊ดก็
หน้า แรกCode (PHP)
<tr>
<td align="right">จังหวัด :</td>
<td><font id="province"><select><option value="0">=========================</option></select></font></td>
</tr>
<tr>
<td align="right">อำเภอ/เขต :</td>
<td><font id="amphur"><select><option value="0">=========================</option></select></font></td>
</tr>
<tr>
<td align="right">ตำบล/แขวง :</td>
<td><font id="tumbon"><select><option value="0">=========================</option></select></font></td>
</tr>
หน้า script ajax
Code (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", "province.php?data="+src+"&val="+val); //สร้าง connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
req.send(null); //ส่งค่า
}
window.onLoad=dochange('province', -1);
แล้วก็หน้า province.php
Code (PHP)
header("content-type: text/html; charset=utf-8");
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");
include "connect.php";
conndb();
$data = $_GET['data'];
$val = $_GET['val'];
if ($data=='province') {
echo "<select name='province' onChange=\"dochange('amphur', this.value)\">\n";
echo "<option value='0'>==== กรุณาเลือกจังหวัด ====</option>\n";
$result=mysql_db_query($dbname,"select * from province order by ProvinceName");
while($row = mysql_fetch_array($result)){
echo "<option value=\"$row[ProvinceID]\" >$row[ProvinceName]</option> \n" ;
}
} else if ($data=='amphur') {
echo "<select name='amphur' onChange=\"dochange('tumbon', this.value)\">\n";
echo "<option value='0'>========= กรุณาเลือกอำเภอ =========</option>\n";
$result=mysql_db_query($dbname,"SELECT * FROM amphur WHERE ProvinceID = '$val' ORDER BY AmphurName");
while($row = mysql_fetch_array($result)){
echo "<option value=\"$row[AmphurID]\" >$row[AmphurName]</option> \n" ;
}
} else if ($data=='tumbon') {
echo "<select name='tumbon'>\n";
echo "<option value='0'>========= กรุณาเลือกตำบล =========</option>\n";
$result=mysql_db_query($dbname,"SELECT * FROM tumbon WHERE AmphurID = '$val' ORDER BY TumbonName");
while($row = mysql_fetch_array($result)){
echo "<option value=\"$row[TumbonID]\" >$row[TumbonName]</option> \n" ;
}
}
echo "</select>\n";
echo mysql_error();
closedb();
|
ประวัติการแก้ไข 2011-12-23 16:29:19
 |
 |
 |
 |
Date :
2011-12-23 16:24:57 |
By :
ichiko |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมเดาเอาว่า ตรงส่วนนี้เนี่ย ให้คุณใส่ข้อมูล ของ จังหวัดลงไปก่อนเลย
<tr>
<td align="right">จังหวัด :</td>
<td><font id="province"><select><option value="0">=========================</option>
// query ข้อมูลของจังหวัด ใส่เข้าไปในนี้ก่อน
</select></font></td>
</tr>
ลองดูครับ
|
 |
 |
 |
 |
Date :
2011-12-23 16:37:37 |
By :
kalamell |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|