Như lời bạn TG đã nói. Sau khi mình về làm bằng CSDL MySQL chạy rất tốt thì hôm nay mình post lên cho các bạn tham khảo ha.
CSDL của mình là tinhthanh gồm 2 bảng: tinh, huyen. Có cấu trúc như sau:
--
-- Table structure for table `huyen`
--
CREATE TABLE `huyen` (
`mahuyen` bigint(20) NOT NULL auto_increment,
`matinh` bigint(20) NOT NULL default '0',
`tenhuyen` varchar(250) NOT NULL default '',
PRIMARY KEY (`mahuyen`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
--
-- Dumping data for table `huyen`
--
INSERT INTO `huyen` VALUES (1, 1, 'Cau Giay');
INSERT INTO `huyen` VALUES (2, 1, 'Ba Dinh');
INSERT INTO `huyen` VALUES (3, 2, 'Vu Thu');
INSERT INTO `huyen` VALUES (4, 2, 'Tien Hai');
INSERT INTO `huyen` VALUES (5, 3, 'DaiTu');
INSERT INTO `huyen` VALUES (6, 3, 'Phu Luong');
-- --------------------------------------------------------
--
-- Table structure for table `tinh`
--
CREATE TABLE `tinh` (
`matinh` bigint(20) NOT NULL auto_increment,
`tentinh` varchar(250) NOT NULL default '',
PRIMARY KEY (`matinh`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
--
-- Dumping data for table `tinh`
--
INSERT INTO `tinh` VALUES (1, 'Ha Noi');
INSERT INTO `tinh` VALUES (2, 'Thai Binh');
INSERT INTO `tinh` VALUES (3, 'Thai Nguyen');
Đây là file: mysql_connect.php
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '12345678');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'tinhthanh');
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
?>
<?
include('mysql_connect.php');
?>
<!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=iso-8859-1" />
<title>AJAX/PHP - PHP BASIC</title>
<script>
loadstatustext = 'Loading...';
function ajaxLoad(url,id)
{
if (document.getElementById) {
var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}
if (x)
{
x.onreadystatechange = function()
{
el = document.getElementById(id);
el.innerHTML = loadstatustext;
if (x.readyState == 4 && x.status == 200)
{
el.innerHTML = x.responseText;
}
}
x.open("GET", url, true);
x.send(null);
}
}
// code demo by www.phpbasic.com
function pb_display(x)
{
ajaxLoad('display.php?town='+x,'huyen');
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="index.php">
<input type="text" name="tinh" />
<!-- Begin: Hien thi phan chon cac tinh thanh -->
<select name="select" onchange="pb_display(this.value);">
<option value="" selected="selected">Chon ti~nh tha`nh</option>
$query_tinh=mysql_query("SELECT * FROM tinh ");
while($row_tinh=mysql_fetch_array($query_tinh))
{
echo "<option value=$row_tinh[matinh]>$row_tinh[tentinh]</option>" ;
}
mysql_free_result($query_tinh);
?>
</select>
<!-- End: Hien thi phan chon cac tinh thanh -->
<!-- Begin: hien thi ca'c quan/huyen tuong u'ng voi ti~nh tha`nh -->
<div id="huyen">Hien thi cac huyen tuong ung cho tu`ng ti~nh tha`nh</div>
<!-- End: hien thi ca'c quan/huyen tuong u'ng voi ti~nh tha`nh -->
<input type="submit" value="Nhap" name="nhap" />
</form>
</body>
</html>
[color="blue">Đây là file: display.php
<?php
# code demo by www.phpbasic.com
# file na`y hie^~n thi cac huyen tuong u'ng voi ca'c ti~nh tha`nh
include('mysql_connect.php');
$get = $_GET['town'];
print '<select name="select">';
$query_huyen=mysql_query("SELECT * FROM huyen WHERE matinh=$get ");
while($row_huyen=mysql_fetch_array($query_huyen))
{
echo "<option value=$row_huyen[mahuyen]>$row_huyen[tenhuyen]</option>" ;
}
mysql_free_result($query_huyen);
print '</select>';
?>
* Ở đây mình sử dụng localhost, password là : 123456 , tên CSDL là tinhthanh. Các bạn có thể thay đổi lại ở file mysql_connect.php để cho phù hợp. Các bạn tham khảo và cho ý kiến nhé. Mình cảm ơn
Tác giả:NguyÅn Minh Tân
