You are on page 1of 40

BÀI 3

<table width="200" border="0" cellspacing="0" cellpadding="0">


<?php
require_once("dbcon.php");
$s1="select * from chuyenmuc";
$kq1=mysql_query($s1,$link);
while ($d1=mysql_fetch_array($kq1))
{
?>
<tr>
<td><?php echo $d1["tencm"] ?></td>
</tr>
<?php
$s2="select * from loaitin where macm='".$d1["macm"]."'";
$kq2=mysql_query($s2,$link);
if (mysql_num_rows($kq2)>0)
{
?>
<tr>
<td id="nut" style="display:none"><table width="200" border="0" cellspacing="0" cellpadding="0">
<?php
while ($d2=mysql_fetch_array($kq2))
{
?>
<tr>
<td><?php echo $d2["tenloai"]; ?></td>
</tr>
<?php } ?>
</table></td>
</tr>
<?php }
}
?>
</table>
<body>
<table width="200" border="0" cellspacing="0" cellpadding="0">
<?php
require_once("dbcon.php");
$s1="select * from chuyenmuc";
$kq1=mysql_query($s1,$link);
$i=0;
while ($d1=mysql_fetch_array($kq1))
{ $s2="select * from loaitin where macm='".$d1["macm"]."'";
$kq2=mysql_query($s2,$link);
if (mysql_num_rows($kq2)==0)
{
?>
<tr>
<td><?php echo $d1["tencm"] ?></td>
</tr>
<?php
}
else
{ $i++;
1
?>
<tr>
<td><a href="javascript:toogle(nut<?php echo $i; ?>)"><?php echo $d1["tencm"] ?></a></td>
</tr>
<tr>
<td id="nut<?php echo $i ?>" style="display:none"><table width="200" border="0" cellspacing="0"
cellpadding="0">
<?php
while ($d2=mysql_fetch_array($kq2))
{
?>
<tr>
<td><?php echo $d2["tenloai"]; ?></td>
</tr>
<?php } ?>
</table></td>
</tr>
<?php
} //ket thuc else
}// ket thuc while
?>
</table>
Index.php
<table width="775" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="t1"><?php require("header.php") ?></td>
</tr>
<tr>
<td class="t2"><table width="775" border="0" cellspacing="0" cellpadding="0">
<tr>

<td width="775" valign="top" class="t4"><?php require("datbao.php") ?></td>

</tr>
</table></td>
</tr>
<tr>
<td bgcolor="#FFFFCC" class="t3"><?php require("footer.php") ?></td>
</tr>
</table>
Datbao.php
<body>
<div align="center"><font size="4" color="#0000ff"><b>Đăng Ký Đặt Báo</b></font></div>
<form name="f" method="post" action="index.php">
<table width="445" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="145"><span class="style5">Loại Báo</span></td>
<td width="300">
<select name="loaibao">
<?php
require_once("dbcon.inc");
mysql_query("SET NAMES 'utf8'");
$sql="select * from loaibao";
$result=mysql_query($sql,$link);

2
if (mysql_num_rows($result)>0) {
while($row=mysql_fetch_array($result)) {
$x1=$row["mabao"];
$x2=$row["tenbao"];
?>
<option value="<?php echo $x1 ?>"><?php echo $x2 ?></option>
<?php
}
}
?>
</select> </td>
</tr>
<tr>
<td width="145"><span class="style5">Số Lượng</span></td>
<td width="300">
<input type="text" name="soluong" size="5" onKeyUp="CheckNumber(this)" value="1"> </td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input name="Button" type="button" class="style3" onClick="kiemtra()" value="Đưa Vào Giỏ
Hàng">
</div> </td>
</tr>
</table>
</form>
<?php
if (isset($HTTP_POST_VARS["loaibao"])) {
$kt=0;
for($i=1;$i<=$_SESSION["tongsl"];$i++){
if ($HTTP_POST_VARS["loaibao"]==$_SESSION["mabao".$i]){
$kt=1;
break;
}
}
if ($kt==0){
$sql="select * from loaibao where mabao='".$HTTP_POST_VARS["loaibao"]."'";
$result=mysql_query($sql,$link);
if (mysql_num_rows($result)>0){
$row=mysql_fetch_array($result);
$_SESSION["tongsl"]++;
$i=$_SESSION["tongsl"];
session_register("mabao".$i);
$_SESSION["mabao".$i]=$row["mabao"];
session_register("tenbao".$i);
$_SESSION["tenbao".$i]=$row["tenbao"];
session_register("gia".$i);
$_SESSION["gia".$i]=$row["gia"];
session_register("soluong".$i);
$_SESSION["soluong".$i]=$HTTP_POST_VARS["soluong"];
}
}
}
if ($_SESSION["tongsl"]>0) {

3
?>
<form method="post" action="capnhatdatbao.php">
<table width="495" border="1" align="center" cellpadding="0" cellspacing="0"
bordercolorlight="#FFFF00">
<tr bgcolor="#CCFFCC">
<td colspan="6"><div align="center" class="style3">Danh Sách Các Loại Báo Cần Đặt</div></td>
</tr>
<tr bgcolor="#CCFFCC">
<td width="45"><div align="center" class="style2"><strong>STT</strong></div></td>
<td width="150"><div align="center" class="style2"><strong>Tên Báo</strong></div></td>
<td width="75"><div align="center" class="style2"><strong>Giá</strong></div></td>
<td width="75"><div align="center" class="style2"><strong>Số Lượng</strong></div></td>
<td width="100"><div align="center" class="style2"><strong>Thành Tiền</strong></div></td>
<td width="50"><div align="center" class="style2"><strong>Xóa</strong></div></td>
</tr>
<?php
for($i=1;$i<=$_SESSION["tongsl"];$i++) {
?>
<tr>
<td width="45"><div align="center"><?php echo $i ?></div></td>
<td width="175"><div align="center"><?php echo $_SESSION["tenbao".$i] ?></div></td>
<td width="50"><div align="right"><?php echo $_SESSION["gia".$i] ?></div></td>
<td width="75"><div align="center"><input type="text" size="5" onKeyUp="CheckNumber(this)"
name="C<?php echo $i ?>" value="<?php echo $_SESSION["soluong".$i] ?>"></div></td>
<td width="100"><div align="right"><?php echo $_SESSION["gia".$i]*$_SESSION["soluong".$i] ?
></div></td>
<td width="50"><div align="center"><a href="xoadatbao.php?id=<?php echo $i ?
>">xóa</a></div></td>
</tr>

<?php
}
?>
</table>
<div align="center">
<input name="Button1" type="submit" class="style3" value="Cập Nhật">
&nbsp;
<input name="Button2" type="button" class="style3" onClick="location.href='order.php'" value="Đặt
Báo">
</div>
</form>
<?php
}
?>
</body>
Xoadatbao.php
<?php
session_start();
for($i=$HTTP_GET_VARS["id"];$i<$_SESSION["tongsl"];$i++){
$j=$i+1;
$_SESSION["mabao".$i]=$_SESSION["mabao".$j];
$_SESSION["tenbao".$i]=$_SESSION["tenbao".$j];
$_SESSION["soluong".$i]=$_SESSION["soluong".$j];
}

4
session_unregister("mabao".$_SESSION["tongsl"]);
session_unregister("tenbao".$_SESSION["tongsl"]);
session_unregister("soluong".$_SESSION["tongsl"]);
session_unregister("gia".$_SESSION["tongsl"]);
$_SESSION["tongsl"]--;
echo "<script>location.href='index.php';</script>";
?>
Capnhatdatbao.php
<?php
session_start();
for($i=1;$i<=$_SESSION["tongsl"];$i++){
if ($HTTP_POST_VARS["C".$i]=="") {
$_SESSION["soluong".$i]=1;
} else {
$_SESSION["soluong".$i]=$HTTP_POST_VARS["C".$i];
}
}
echo "<script>location.href='index.php';</script>"; ?>

BÀI 4
Luu_order.php
<?php
session_start();
require_once("dbcon.inc");
mysql_query("SET NAMES 'utf8'");
$ngay =
mktime(0,0,0,intval($HTTP_POST_VARS["thang"]),intval($HTTP_POST_VARS["ngay"]),intval($HTTP_
POST_VARS["nam"]));
$ngay1=date("Y-m-d",$ngay);
$sql="insert into datbao(hoten, diachi, sodt, ngaydk, trangthai) values('";
$sql .=$HTTP_POST_VARS["T1"]."','".$HTTP_POST_VARS["T2"]."','".
$HTTP_POST_VARS["T3"]."','".$ngay1."',false)";
$result=mysql_query($sql,$link);
if ($result) {
$sql="select * from datbao order by madatbao desc";
$result=mysql_query($sql,$link);
if (mysql_num_rows($result)>0){
$row=mysql_fetch_array($result);
$ma=$row["madatbao"];
//echo $_SESSION["tongsl"];
for($i=1;$i<=$_SESSION["tongsl"];$i++){
$sql="insert into ct_datbao(madatbao, mabao, soluong, gia) values(";
$sql .=$ma.",'".$_SESSION["mabao".$i]."',".$_SESSION["soluong".$i].",".
$_SESSION["gia".$i].")";
$result=mysql_query($sql,$link);
}
}
}
for($i=1;$i<=$_SESSION["tongsl"];$i++){
session_unregister("mabao".$i);
session_unregister("tenbao".$i);
session_unregister("soluong".$i);
session_unregister("gia".$i);
}
5
$_SESSION["tongsl"]=0;
echo "<script>alert('Da dat bao thanh cong');location.href='index.php';</script>";
?>
Toogle.php
<script language="javascript">
function toogle(m,n)
{ for(i=1;i<=n;i++)
{ k=document.getElementById("nut"+i);
if (m==i) {
k.style.display="inline";
}
else
{
k.style.display="none";
}
}
}
</script>
</head>

<body>
<table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="left"><a href="javascript:toogle(1,3)">the thao</a></div></td>
</tr>
<tr>
<td id="nut1" style="display:none"><table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>the thao 1 </td>
</tr>
<tr>
<td>the thao 2 </td>
</tr>
<tr>
<td>the thao 3 </td>
</tr>
<tr>
<td>the thao 4 </td>
</tr>
</table></td>
</tr>
<tr>
<td><a href="javascript:toogle(2,3)">van hoa</a></td>
</tr>
<tr>
<td id="nut2" style="display:none"><table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>vh 1</td>
</tr>
<tr>
<td>vj2</td>
</tr>
<tr>
<td>vh3</td>

6
</tr>
</table></td>
</tr>
<tr>
<td><a href="javascript:toogle(3,3)">Xa Hoi</a></td>
</tr>
<tr>
<td id="nut3" style="display:none"><table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>xa hoi 1</td>
</tr>
<tr>
<td>xa hoi 2</td>
</tr>
</table></td>
</tr>

</table>
</body>
</html>

Hanghoa.php
<body>
<table width="200" border="0" align="center" cellpadding="0" cellspacing="0">
<?php
require_once("dbcon.php");
$kq=mysql_query("select * from hanghoa",$link);
$sodong=2*2;
$sotrang=ceil(mysql_num_rows($kq)/$sodong);
$nhom=3;
$sonhom=ceil($sotrang/$nhom);
if (!isset($HTTP_GET_VARS["p"]))
{
$p=1;
}
else
{
$p=intval($HTTP_GET_VARS["p"]);
}
$n=ceil($p/$nhom);
if (isset($_GET["n"]))
{
$n=intval($_GET["n"]);
$p=$n*$nhom-$nhom+1;
}

$x=$sodong*($p-1);
$s="select * from hanghoa limit ".$x.",".$sodong;
$kq=mysql_query($s,$link);
while ($d=mysql_fetch_array($kq))
{
?>
<tr>
<td>

7
<img witdh="50" height="50" src="hinh/<?php echo $d["hinh"]; ?>" /><br />
<?php echo $d["tenhang"]; ?><br />
<?php echo $d["gia"]; ?><br />
<a href="giohang1.php?m=<?php echo $d["mahang"];?>">Mua Hàng</a>
</td>
<?php
if ($d=mysql_fetch_array($kq))
{
?>
<td>
<img witdh="50" height="50" src="hinh/<?php echo $d["hinh"]; ?>" /><br />
<?php echo $d["tenhang"]; ?><br />
<?php echo $d["gia"]; ?><br />
<a href="giohang1.php?m=<?php echo $d["mahang"];?>">Mua Hàng</a>
</td>
<?php
} else {
?>
<td>&nbsp;</td>
<?php
}
?>
</tr>
<?php
}//while
?>
</table>
<p align="center">
<?php
if ($n>1)
{ $n1=$n-1;
?>
<a href="hanghoa.php?n=<?php echo $n1; ?>"><<</a>&nbsp;
<?php
}

$dau=$n*$nhom-$nhom+1;
if ($n<$sonhom)
{
$cuoi=$n*$nhom;
}
else
{
$cuoi=$sotrang;
}
for($i=$dau;$i<=$cuoi;$i++)
{
if ($p==$i)
{
echo $i."&nbsp;";
}
else
{
?>

8
<a href="hanghoa.php?p=<?php echo $i ?>"><?php echo $i;?></a>

<?php
}
}
if ($n<$sonhom)
{ $n2=$n+1;
?>
<a href="hanghoa.php?n=<?php echo $n2; ?>">>></a>
<?php
}
?>
</p>
</body>
</html>
Giohang.php
<body>
<?php
require_once("dbcon.php");
if (isset($HTTP_GET_VARS["m"])) {
$kt=0;
for($i=1;$i<=$_SESSION["tongsl"];$i++){
if ($HTTP_GET_VARS["m"]==$_SESSION["mahang".$i]){
$kt=1;
break;
}
}
if ($kt==0){
$sql="select * from hanghoa where mahang='".$HTTP_GET_VARS["m"]."'";
$result=mysql_query($sql,$link);
if (mysql_num_rows($result)>0){
$row=mysql_fetch_array($result);
$_SESSION["tongsl"]++;
$i=$_SESSION["tongsl"];
session_register("mahang".$i);
$_SESSION["mahang".$i]=$row["mahang"];
session_register("tenhang".$i);
$_SESSION["tenhang".$i]=$row["tenhang"];
session_register("gia".$i);
$_SESSION["gia".$i]=$row["gia"];
session_register("soluong".$i);
$_SESSION["soluong".$i]=1;
}
}
}
if ($_SESSION["tongsl"]>0) {
?>
<form method="post" action="capnhatdathang.php">
<table width="495" border="1" align="center" cellpadding="0" cellspacing="0"
bordercolorlight="#FFFF00">
<tr bgcolor="#CCFFCC">
<td colspan="6"><div align="center" class="style3">Danh Sách Các Mặt Hàng Cần Mua</div></td>
</tr>
<tr bgcolor="#CCFFCC">

9
<td width="45"><div align="center" class="style2"><strong>STT</strong></div></td>
<td width="150"><div align="center" class="style2"><strong>Tên Hàng</strong></div></td>
<td width="75"><div align="center" class="style2"><strong>Giá</strong></div></td>
<td width="75"><div align="center" class="style2"><strong>Số Lượng</strong></div></td>
<td width="100"><div align="center" class="style2"><strong>Thành Tiền</strong></div></td>
<td width="50"><div align="center" class="style2"><strong>Xóa</strong></div></td>
</tr>
<?php
for($i=1;$i<=$_SESSION["tongsl"];$i++) {
?>
<tr>
<td width="45"><div align="center"><?php echo $i ?></div></td>
<td width="175"><div align="center"><?php echo $_SESSION["tenhang".$i] ?></div></td>
<td width="50"><div align="right"><?php echo $_SESSION["gia".$i] ?></div></td>
<td width="75"><div align="center"><input type="text" size="5" onKeyUp="CheckNumber(this)"
name="C<?php echo $i ?>" value="<?php echo $_SESSION["soluong".$i] ?>"></div></td>
<td width="100"><div align="right"><?php echo $_SESSION["gia".$i]*$_SESSION["soluong".$i] ?
></div></td>
<td width="50"><div align="center"><a href="xoadathang.php?id=<?php echo $i ?
>">xóa</a></div></td>
</tr>

<?php
}
?>
</table>
<div align="center">
<input name="Button1" type="submit" class="style3" value="Cập Nhật">
&nbsp;
<input name="Button2" type="button" class="style3" onClick="location.href='order.php'" value="Đặt
Hàng">
&nbsp;
<input name="button" type="button" class="style3" id="button" onClick="location.href='hanghoa.php'"
value="chọn tiếp" >
</div>
</form>
<?php
}
?>
</body>
Giohang1.php
<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><?php require_once("giohang.php"); ?></td>
</tr>
</table>
Capnhatdathang.php
<?php
session_start();
for($i=1;$i<=$_SESSION["tongsl"];$i++){
if ($HTTP_POST_VARS["C".$i]=="") {
$_SESSION["soluong".$i]=1;
}else

10
{
$_SESSION["soluong".$i]=$HTTP_POST_VARS["C".$i];
}
}
echo "<script>location.href='giohang1.php';</script>";
?>

Xoadathang.php
<?php
session_start();
for($i=$HTTP_GET_VARS["id"];$i<$_SESSION["tongsl"];$i++){
$j=$i+1;
$_SESSION["mahang".$i]=$_SESSION["mahang".$j];
$_SESSION["tenhang".$i]=$_SESSION["tenhang".$j];
$_SESSION["soluong".$i]=$_SESSION["soluong".$j];
}
session_unregister("mahang".$_SESSION["tongsl"]);
session_unregister("tenhang".$_SESSION["tongsl"]);
session_unregister("soluong".$_SESSION["tongsl"]);
session_unregister("gia".$_SESSION["tongsl"]);
$_SESSION["tongsl"]--;
echo "<script>location.href='giohang1.php';</script>";
?>
BÀI 5
Admin_dienvien.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p align="center">Danh Sách Các Diễn Viên<br>
<a href="them_dienvien.php">Thêm Mới</a> <a href="admin.php">Trang Admin </a></p>
<table width="350" border="1" align="center" cellpadding="0" cellspacing="0">
<tr bgcolor="#FFFFCC">
<td width="50"><div align="center" class="style1">STT</div></td>
<td width="75"><div align="center" class="style1">Mã DV </div></td>
<td width="125"><div align="center" class="style1">Tên Diễn Viên </div></td>
<td width="100"><div align="center" class="style1">Hình</div></td>
<td width="50"><div align="center" class="style1">Sửa</div></td>
<td width="50"><div align="center" class="style1">Xóa</div></td>
</tr>
<?php
require_once("../dbconnect.inc");
$sql="select * from dienvien";
$kq=mysql_query($sql,$link);
$i=0;
while($dong=mysql_fetch_array($kq))
{ $i++;
?>
11
<tr>
<td width="50"><div align="center"><?php echo $i; ?></div></td>
<td width="75"><div align="center"><?php echo $dong["madv"]; ?></div></td>
<td width="125"><div align="center"><?php echo $dong["hoten"]; ?></div></td>
<td width="100"><div align="center"><img src="../images/<?php echo $dong["hinh"]; ?>"
width="75" height="75"></div></td>
<td width="50"><div align="center"><a href="sua_dienvien.php?id=<?php echo $dong["madv"]; ?
>">Sửa</a></div></td>
<td width="50"><div align="center"><a href="xoa_dienvien.php?id=<?php echo $dong["madv"]; ?
>">Xóa</a></div></td>
</tr>
<?php
}
?>
</table>
<p>&nbsp;</p>
</body>
</html>
Them_dienvien.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="luu_them_dienvien.php" method="post" enctype="multipart/form-data" name="f">
<table width="350" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><div align="center">Thêm Diễn Viên</div></td>
</tr>
<tr>
<td width="100">Mã Diễn Viên</td>
<td width="250"><input name="txtmadv" type="text" id="txtmadv"></td>
</tr>
<tr>
<td width="100">Tên Diễn Viên</td>
<td width="250"><input name="txthoten" type="text" id="txthoten"></td>
</tr>
<tr>
<td width="100">Phái</td>
<td width="250">Nam
<input name="txtphai" type="radio" value="1" checked="checked" />
Nữ
<input name="txtphai" type="radio" value="0" /></td>
</tr>
<tr>
<td width="100">Hình</td>
<td width="250"><input name="txthinh" type="file" id="txthinh"></td>
</tr>
<tr>
<td colspan="2"><div align="center">

12
<input type="submit" name="Submit" value="Lưu">
<input type="button" name="Submit2" value="Quay Về" onClick="history.back()">
</div></td>
</tr>
</table>
</form>
</body>
</html>
Luu_them_dienvien.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
require_once("../dbconnect.inc");
if ($HTTP_POST_VARS["txtphai"]=="1")
$phai=1;
else
$phai=0;
$sql="insert into dienvien(madv, hoten, hinh, phai) values('".$HTTP_POST_VARS["txtmadv"]."','".
$HTTP_POST_VARS["txthoten"]."','".$_FILES["txthinh"]["name"]."',".$phai.")";
$kq=mysql_query($sql,$link);
if ($kq)
{
echo "<script>alert('Them thanh cong');location.href='admin_dienvien.php';</script>";
move_uploaded_file($_FILES["txthinh"]["tmp_name"],"../images/".$_FILES["txthinh"]["name"]);
}
else
{
echo "<script>alert('Them khong thanh cong');location.href='admin_dienvien.php';</script>";
}
?>

</body>
</html>
Sua_dienvien.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
require_once("../dbconnect.inc");
$sql="select * from dienvien where madv='".$HTTP_GET_VARS["id"]."'";
$kq=mysql_query($sql,$link);

13
if (mysql_num_rows($kq)>0)
{
$dong=mysql_fetch_array($kq);
$madv=$dong["madv"];
$hoten=$dong["hoten"];
$hinh=$dong["hinh"];
$phai=$dong["phai"];
}
else
{
$madv="";
$hoten="";
$hinh="";
$phai=1;
}
?>
<form action="luu_sua_dienvien.php" method="post" enctype="multipart/form-data" name="f">
<table width="350" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><div align="center">Thay Đổi Thông Tin Diễn Viên</div></td>
</tr>
<tr>
<td width="100">Mã Diễn Viên</td>
<td width="250">
<input name="txtmadv" type="text" id="txtmadv" value="<?php echo $madv; ?>" disabled>
<input name="txtmadv1" type="hidden" value="<?php echo $madv; ?>">
</td>
</tr>
<tr>
<td width="100">Tên Diễn Viên</td>
<td width="250"><input name="txthoten" type="text" id="txthoten" value="<?php echo $hoten; ?
>"></td>
</tr>
<tr>
<td width="100">Phái</td>
<td width="250">
<?php if ($phai==1)
{
?>
Nam
<input name="txtphai" type="radio" value="1" checked="checked" />
Nữ
<input name="txtphai" type="radio" value="0" />
<?php
}
else
{
?>
Nam
<input name="txtphai" type="radio" value="1" />
Nữ
<input name="txtphai" type="radio" value="0" checked="checked"/>
<?php
}

14
?>
</td>
</tr>
<tr>
<td width="100">Hình</td>
<td width="250"><input name="txthinh" type="file" id="txthinh"><br>
<img src="../images/<?php echo $hinh; ?>" width="50" height="50">
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="Lưu">
<input type="button" name="Submit2" value="Quay Về" onClick="history.back()">
</div></td>
</tr>
</table>
</form>
</body>
</html>
Luu_sua_dienvien.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
require_once("../dbconnect.inc");
if ($HTTP_POST_VARS["txtphai"]=="1")
$phai=1;
else
$phai=0;
if ($_FILES["txthinh"]["name"]=="")
{
$sql="update dienvien set hoten='".$HTTP_POST_VARS["txthoten"]."', ";
$sql.="phai=".$phai;
$sql .=" where madv='".$HTTP_POST_VARS["txtmadv1"]."'";
}
else
{
$sql="update dienvien set hoten='".$HTTP_POST_VARS["txthoten"]."', hinh='".$_FILES["txthinh"]
["name"]."', ";
$sql.="phai=".$phai;
$sql.=" where madv='".$HTTP_POST_VARS["txtmadv1"]."'";
move_uploaded_file($_FILES["txthinh"]["tmp_name"],"../images/".$_FILES["txthinh"]["name"]);
}

$kq=mysql_query($sql,$link);
if ($kq)
{
echo "<script>alert('Sua thanh cong');location.href='admin_dienvien.php';</script>";

15
}
else
{
echo "<script>alert('Sua khong thanh cong');location.href='admin_dienvien.php';</script>";
}
?>
</body>
</html>
Xoa_dienvien.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
require_once("../dbconnect.inc");
$sql="delete from dienvien where madv='".$HTTP_GET_VARS["id"]."'";
$kq=mysql_query($sql,$link);
if ($kq)
{
echo "<script>alert('Xoa thanh cong');location.href='admin_dienvien.php';</script>";
}
else
{
echo "<script>alert('Xoa khong thanh cong');location.href='admin_dienvien.php';</script>";
}
?>

</body>
</html>
Admin_lichchieu.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p align="center">Danh Sách Lịch Chiếu<br>
<a href="them_lichchieu.php">Thêm Mới</a> <a href="admin.php">Trang Admin </a></p>
<table width="500" border="1" align="center" cellpadding="0" cellspacing="0">
<tr bgcolor="#FFFFCC">
<td width="50"><div align="center" class="style1">STT</div></td>
<td width="100"><div align="center" class="style1">Phim</div></td>
<td width="125"><div align="center" class="style1">Từ Ngày</div></td>
<td width="125"><div align="center" class="style1">Đến Ngày</div></td>
<td width="50"><div align="center" class="style1">Sửa</div></td>
<td width="50"><div align="center" class="style1">Xóa</div></td>

16
</tr>
<?php
require_once("../dbconnect.inc");
$sql="select mald,tenphim,tungay,denngay from lichdien,phim where phim.maphim=lichdien.maphim";
$kq=mysql_query($sql,$link);
$i=0;
while($dong=mysql_fetch_array($kq))
{ $i++;
$t1=substr($dong[denngay],0,4);
$t2=substr($dong[denngay],5,7);
$t3=substr($dong[denngay],8,10);
$ngay=mktime(0,0,0,intval($t2),intval($t3),intval($t1));
$ngay1=date("d/m/Y",$ngay);

$t4=substr($dong[tungay],0,4);
$t5=substr($dong[tungay],5,7);
$t6=substr($dong[tungay],8,10);
$ngay=mktime(0,0,0,intval($t5),intval($t6),intval($t4));
$ngay2=date("d/m/Y",$ngay);
?>
<tr>
<td width="50"><div align="center"><?php echo $i; ?></div></td>
<td width="100"><div align="center"><?php echo $dong["tenphim"]; ?></div></td>
<td width="125"><div align="center"><?php echo $ngay2; ?></div></td>
<td width="125"><div align="center"><?php echo $ngay1; ?></div></td>
<td width="50"><div align="center"><a href="sua_lichchieu.php?id=<?php echo $dong["mald"]; ?
>">Sửa</a></div></td>
<td width="50"><div align="center"><a href="xoa_lichchieu.php?id=<?php echo $dong["mald"]; ?
>">Xóa</a></div></td>
</tr>
<?php
}
?>
</table>
<p>&nbsp;</p>
</body>
</html>
Them_lichchieu.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=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="popcalendar.js"></script>
</head>

<body>
<form name="f" method="post" action="luu_them_lichchieu.php">
<table width="300" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><div align="center">Thêm Lịch Chiếu </div></td>
</tr>
<tr>

17
<td width="100">Phim</td>
<td width="100"><select name="txtmaphim">
<?php
require_once("../dbconnect.inc");
$sql="select * from phim";
$kq=mysql_query($sql,$link);
while($dong=mysql_fetch_array($kq))
{
?>
<option value="<?php echo $dong["maphim"]; ?>"><?php echo $dong["tenphim"]; ?></option>

<?php
}
?>
</select></td>
</tr>
<tr>
<td width="100">Từ Ngày</td>
<td width="250"><input name="txttungay" type="text" id="txttungay">
<img src="../images/calendar.jpg" width="36" height="20" align="absmiddle"
onClick="popUpCalendar(this, document.all.txttungay, 'yyyy-m-d', 0, 0)">
</td>
</tr>
<tr>
<td width="100">Đến Ngày</td>
<td width="250"><input name="txtdenngay" type="text" id="txtdenngay">
<img src="../images/calendar.jpg" width="36" height="20" align="absmiddle"
onClick="popUpCalendar(this, document.all.txtdenngay, 'yyyy-m-d', 0, 0)">
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="Lưu">
<input type="button" name="Submit2" value="Quay Về" onClick="history.back()">
</div></td>
</tr>
</table>
</form>
</body>
</html>
Luu_them_lichchieu.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
require_once("../dbconnect.inc");
$sql="insert into lichdien(maphim,tungay,denngay) values('".$HTTP_POST_VARS["txtmaphim"]."','".
$HTTP_POST_VARS["txttungay"]."','".$HTTP_POST_VARS["txtdenngay"]."')";

18
$kq=mysql_query($sql,$link);
if ($kq)
{
echo "<script>alert('Them thanh cong');location.href='admin_lichchieu.php';</script>";
}
else
{
echo "<script>alert('Them khong thanh cong');location.href='admin_lichchieu.php';</script>";
}
?>
</body>
</html>
Sua_lichchieu.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=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="popcalendar.js"></script>
</head>

<body>
<?php
require_once("../dbconnect.inc");
$sql="select * from lichdien where mald='".$HTTP_GET_VARS["id"]."'";
$kq=mysql_query($sql,$link);
if (mysql_num_rows($kq)>0)
{
$dong=mysql_fetch_array($kq);
$mald=$dong["mald"];
$maphim=$dong["maphim"];
$tungay=$dong["tungay"];
$denngay=$dong["denngay"];
}
else
{
$mald="";
$maphim="";
$tungay="";
$denngay="";
}
?>
<form name="f" method="post" action="luu_sua_lichchieu.php">
<table width="300" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><div align="center">Thay Đổi Lịch Chiếu </div></td>
</tr>
<tr>
<td width="100">Mã Lịch Chiếu</td>
<td width="200">
<input name="txtmald" type="text" id="txtmald" disabled value="<?php echo $mald; ?>">
<input name="txtmald1" type="hidden" value="<?php echo $mald; ?>">
</td>

19
</tr>
<tr>
<td width="100">Mã Phim</td>
<td width="200">
<select name="txtmaphim">
<?php
require_once("../dbconnect.inc");
$sql="select * from phim";
$kq=mysql_query($sql,$link);
while($dong=mysql_fetch_array($kq))
{ if ($maphim==$dong["maphim"])
{
?>
<option value="<?php echo $dong["maphim"]; ?>" selected><?php echo $dong["tenphim"]; ?
></option>
<?php
}
else
{
?>
<option value="<?php echo $dong["maphim"]; ?>"><?php echo $dong["tenphim"]; ?></option>
<?php
}
}
?>
</select>
</td></tr>
<tr>
<td width="100">Từ Ngày</td>
<td width="200"><input name="txttungay" type="text" id="txttungay" value="<?php echo $tungay; ?
>" />
<img src="../images/calendar.jpg" width="36" height="20" align="absmiddle"
onClick="popUpCalendar(this, document.all.txttungay, 'yyyy-m-d', 0, 0)">
</td></tr>
<tr>
<td width="100">Đến Ngày</td>
<td width="200"><input name="txtdenngay" type="text" id="txtdenngay" value="<?php echo $denngay?
>" />
<img src="../images/calendar.jpg" width="36" height="20" align="absmiddle"
onClick="popUpCalendar(this, document.all.txtdenngay, 'yyyy-m-d', 0, 0)">
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="Lưu">
<input type="button" name="Submit2" value="Quay Về" onClick="history.back()">
</div></td>
</tr>
</table>
</form>
</body>
</html>
Luu_sua_lichchieu.php

20
<!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>

<body>
<?php
require_once("../dbconnect.inc");
$sql="update lichdien set maphim='".$HTTP_POST_VARS["txtmaphim"]."', ";
$sql .="tungay='".$HTTP_POST_VARS["txttungay"]."', ";
$sql .="denngay='".$HTTP_POST_VARS["txtdenngay"]."' ";
$sql .=" where mald=".$HTTP_POST_VARS["txtmald1"];
$kq=mysql_query($sql,$link);
if ($kq)
{
echo "<script>alert('Sua thanh cong');location.href='admin_lichchieu.php';</script>";
}
else
{
echo "<script>alert('Sua khong thanh cong');location.href='admin_lichchieu.php';</script>";
}
?>

</body>
</html>
Xoa_lichchieu.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
require_once("../dbconnect.inc");
$sql="delete from lichdien where mald=".$HTTP_GET_VARS["id"];
$kq=mysql_query($sql,$link);
if ($kq)
{
echo "<script>alert('Xoa thanh cong');location.href='admin_lichchieu.php';</script>";
}
else
{
echo "<script>alert('Xoa khong thanh cong');location.href='admin_lichchieu.php';</script>";
}
?>
</body>
</html>
Visit.php

21
<?php
$filename = "a.txt";

$fileToOpen = fopen($filename,"r");

$content = intval(fread($fileToOpen, filesize($filename)));


fclose($fileToOpen);
chmod($filename,0766);
$fileToOpen = fopen($filename,"w");

if($content == "")
{
$content = 0;
}

$content++;

fputs($fileToOpen,$content);

fclose($fileToOpen);
chmod($filename,0744);

echo($content);
?>
Hàm chmod
chmod(file,mode)

Parameter Description
file Required. Specifies the file to check
mode Required. Specifies the new permissions.

The mode parameter consists of four numbers:

 The first number is always zero


 The second number specifies permissions for the owner
 The third number specifies permissions for the owner's user group
 The fourth number specifies permissions for everybody else

Possible values (to set multiple permissions, add up the following numbers):

 1 = execute permissions
 2 = write permissions

 4 = read permissions

Example
<?php
// Read and write for owner, nothing for everybody else
chmod("test.txt",0600);
// Read and write for owner, read for everybody else
chmod("test.txt",0644);
// Everything for owner, read and execute for everybody else
chmod("test.txt",0755);
// Everything for owner, read for owner's group
chmod("test.txt",0740);
?>

22
Useronline.php
<?php
$link=mysql_connect("localhost", "root", "") or die("khong the ket noi");
mysql_select_db("online") or die("khong tim thay csdl");

$tg1= date("U");
$tg2=$tg1-600;

$ip=$_SERVER["REMOTE_ADDR"];

$s1 = "DELETE FROM useronline where ip='".$ip."'";


$kq1 = mysql_query($s1,$link) or die();
$s2 = "DELETE FROM useronline where tg<".$tg2;
$kq2 = mysql_query($s2,$link) or die();

$s3 = "INSERT into useronline(ip, tg) VALUES ('".$ip."',".$tg1.")";


$kq3 = mysql_query($s3,$link) or die();

$kq = mysql_query("SELECT * FROM useronline", $link);


$so_user = mysql_num_rows($kq);

echo $so_user;
?>
BÀI 6
Index.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>
<title>AJAX</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles.css" rel="stylesheet" type="text/css" media="screen" />
<script language="javascript" src="scripts.js">
</script>

</head>

<body>
<div id="container">

<div id="theform">
<p class="label">Tên đăng nhập:</p>
<p>
<input id="name" name="username" type="text" class="textForm" width="20"
onfocus="this.className='textForm_Hover';"
onblur="this.className='textForm';checkName(this.value,'');" /><br />
<span class="hidden" id="nameCheckFailed">Tên bạn chọn đã có người khác đăng ký. Vui
lòng chọn một tên đăng nhập khác. </span>
</p>
<p class="label">Password:</p>
<p>
23
<input id="pass1" name="pass1" type="password" class="textForm" width="40"
/><br />
<input id="pass2" name="pass2" type="password" class="textForm" width="40" /><br />
</p>

<p><input id="register" name="register" type="button" disabled class="buttonForm"


value="Đăng ký" onclick="checkpass()" />
</p>
</div>

<div id="the_error_message" class="hidden">


<div class="error_message">
<p>Có lỗi xảy ra khi tạo tài khoản mới</p>
<p>Nhấn vào liên kết dưới đây để quay lại</p>
</div>
<a href="#" onclick="go_back();"><h1>QUAY LẠI MẪU ĐƠN</h1></a>
</div>

<div id="the_welcome_message" class="hidden">


<div class="welcome_message">
<p>Tài khoản đã được tạo thành công.</p
></div>
<a href="index.php"><h1>ĐĂNG KÝ MỚI</h1></a>
</div>
</div>
</body>
</html>

Scripts.js
var req;
var name_OK = false;
function loadXMLDoc(url)
{
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send();
}
}
}

function processReqChange()
{
if (req.readyState == 4) // only if req shows "complete"
{

24
if (req.status == 200) // only if "OK"
{
response = req.responseXML.documentElement;
A_Method = response.getElementsByTagName('method')[0].firstChild.data;
A_Result = response.getElementsByTagName('result')[0].firstChild.data;
if (A_Method!='addNew')
{
eval(A_Method + '(\'\', A_Result)');
}
else
{
eval(A_Method + '(\'\',\'\', A_Result)');
}
}
else
{
alert("There was a problem retrieving the XML data:\n" + req.statusText);
}
}
}
function checkName(input, response)
{

if (response != '')
{
message = document.getElementById('nameCheckFailed');
if (response == 0){
document.getElementById("name").focus();
message.className = 'error';
document.getElementById("name").className="textForm_Error";
name_OK = false;
} else {
message.className = 'hidden';
document.getElementById("name").className="textForm_Accept";
name_OK = true;
}
checkReady();
}
else
{
if(input!='')
{
url = 'process.php?task=1&q=' + input;
loadXMLDoc(url);
}
else
{
name_OK = false;
}
}
}

// Cac ham duoi day deu co nguyen tac hoat dong tuong tu nhu ham checkName o tren

25
function addNew(name, pass1, response)
{
if (response != '')
{

if (response == 0){
document.getElementById("theform").className="hidden";
document.getElementById("the_error_message").className="display";
}
else
{
document.getElementById("theform").className="hidden";
document.getElementById("the_welcome_message").className="display";
}
}
else
{
url = 'process.php?task=2&name=' + name+'&pass='+pass1;
loadXMLDoc(url);
}
}
function checkReady()
{
ButtonObject = document.getElementById('register');
if (name_OK)
{
ButtonObject.disabled = false;
}
else
{
ButtonObject.disabled = true;
}
}

function go_back()
{
document.getElementById("theform").className="display";
document.getElementById("the_error_message").className="hidden";
}
function checkpass()
{
if (document.getElementById("pass1").value!=document.getElementById("pass2").value)
{
alert("password khong chinh xac");
document.getElementById("pass1").focus();
return;
}
addNew(document.getElementById('name').value, document.getElementById('pass1').value,'');
}
Process.php
<?php
header('Content-Type: text/xml');
/* Database variable */
$host = "localhost";

26
$database = "ajax";
$username = "root";
$pass = "";

/* function library */
function connectToDataServer()
{
global $host, $username, $pass;
return mysql_connect($host,$username,$pass);
}
function generateXMLResult($MethodName, $value)
{
print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
print "<response>";
print "<method>".$MethodName."</method>";
print "<result>".$value."</result>";
print "</response>";
}
function checkName($name)
{
global $database;
$error = true;
$link = connectToDataServer();
mysql_select_db($database);
if ($name != "")
{
$SQLString = "SELECT name FROM thanhvien WHERE name='".$name."'";
$result = mysql_query($SQLString);
if (mysql_num_rows($result) == 0) // if no one has the same email
{
$error = false;
}
}
if($error) //there is an error
{ generateXMLResult("checkName",0); }
else //succeed
{ generateXMLResult("checkName",1); }
}

function insert($name, $pass1)


{
global $database;
$error = true;
$link = connectToDataServer();
mysql_select_db($database);
if (($name!="") && ($pass1!=""))
{
$SQLString = "INSERT INTO thanhvien(name, pass) VALUES('".$name."','".$pass1."')";
if(mysql_query($SQLString)) // if add successfully
{
$error=false;
}
}

27
if($error) //there is an error
{ generateXMLResult("addNew",0); }
else //succeed
{ generateXMLResult("addNew",1); }
}

$task = $_GET["task"];
$query = $_GET["q"];
$name = $_GET["name"];
$pass1 = $_GET["pass"];
if ($task!="")
{
switch($task)
{
case '1':
checkName($query);
break;
case '2':
insert($name, $pass1);
break;
}
}
?>
Tinh.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
require_once("dbcon.php");
if (isset($_POST["tinh"]))
{
$tinh=$_POST["tinh"];
}
else
{
$tinh="";
}

?>
<form id="form1" name="form1" method="post" action="tinh.php">
<p>
<select name="tinh" id="tinh" onchange="document.form1.submit()">
<?php
$s1="select * from tinh";
$kq1=mysql_query($s1,$link);
while($d1=mysql_fetch_array($kq1))
{
if ($tinh=="")

28
{$tinh=$d1["matinh"];}
if ($tinh==$d1["matinh"]) {
?>
<option value="<?php echo $d1["matinh"]; ?>" selected><?php echo $d1["tentinh"]; ?></option>
<?php
}
else
{
?>
<option value="<?php echo $d1["matinh"]; ?>"><?php echo $d1["tentinh"]; ?></option>
<?php
}
}
?>
</select>
</p>
<p>
<select name="quan" id="quan">
<?php
$s2="select * from quan where matinh='".$tinh."'";
$kq2=mysql_query($s2,$link);
while($d2=mysql_fetch_array($kq2))
{
if ($quan=="")
{$quan=$d2["maquan"];}
if ($quan==$d2["maquan"]) {
?>
<option value="<?php echo $d2["maquan"]; ?>" selected><?php echo $d2["tenquan"]; ?></option>
<?php
}
else
{
?>
<option value="<?php echo $d2["maquan"]; ?>"><?php echo $d2["tenquan"]; ?></option>
<?php
}
}
?>
</select>
</p>

</form>
</body>
</html>
Tinh.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=utf-8" />
<title>Untitled Document</title>
<script src="scripts.js"></script>
</head>

29
<body>
<?php
require_once("dbcon.php");
?>
<form id="form1" name="form1" method="post">
<p>
<select name="tinh" id="tinh" onchange="loadXMLDoc(this.value)">
<?php
$s1="select * from tinh";
$kq1=mysql_query($s1,$link);
while($d1=mysql_fetch_array($kq1))
{
?>
<option value="<?php echo $d1["matinh"]; ?>" ><?php echo $d1["tentinh"]; ?></option>
<?php
}
?>
</select>
</p>
</form>
</p><div id="quan"></div></p>
</body>
</html>
Scripts.js
var req;
function loadXMLDoc(str)
{
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
var url="process.php";
url=url+"?q="+str;
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send();
}
}
}
function processReqChange()
{
if (req.readyState == 4) // only if req shows "complete"
{
if (req.status == 200) // only if "OK"
{
document.getElementById("quan").innerHTML=req.responseText;
}
}

30
}

Process.php
<?php
require_once("dbcon.php");
$s="select * from quan where matinh='".$_GET["q"]."'";
$kq=mysql_query($s,$link);
echo "<select id='quan'>";
while($d=mysql_fetch_array($kq))
{
echo "<option value='".$d["maquan"]."'>".$d["tenquan"]."</option>";
}
echo "</select>";
?>
Upload file:
<?php
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0) {
echo "loi: " . $_FILES["file"]["error"] . "<br />";
} else {
if (file_exists("upload/" . $_FILES["file"]["name"]))
{ echo $_FILES["file"]["name"] . " already exists. "; }
else { move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . $_FILES["file"]["name"]); }
}
} else { echo "file khong dung dang"; } ?>
BÀI 7
1. Hàm đặc biệt về string

The htmlspecialchars() function converts some predefined characters to HTML entities.

The predefined characters are:

 & (ampersand) becomes &amp;


 " (double quote) becomes &quot;
 ' (single quote) becomes &#039;
 < (less than) becomes &lt;
 > (greater than) becomes &gt;

htmlspecialchars(string,quotestyle)

Parameter Description
string Required. Specifies the string to convert
quotestyle Optional. Specifies how to encode single and double quotes.

The available quote styles are:

 ENT_COMPAT - Default. Encodes only double quotes


 ENT_QUOTES - Encodes double and single quotes

 ENT_NOQUOTES - Does not encode any quotes


31
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT);
echo "<br />";
echo htmlspecialchars($str, ENT_QUOTES);
echo "<br />";
echo htmlspecialchars($str, ENT_NOQUOTES);
?>

str_replace(find,replace,string,count)

Parameter Description
find Required. Specifies the value to find
replace Required. Specifies the value to replace the value in find
string Required. Specifies the string to be searched
count Optional. A variable that counts the number of replacements

<?php
$s=str_repalce(“<script”,””,$_POST[“noidung”]);
?>
Chú ý về upload file

$filename=trim($_FILES["file"]["name"]);
$filename=ereg_replace(" ","",$filename);
if (ereg(".jpg",$filename);)
{
move_uploaded_file(…);
}

2. Đọan code vbscript


<script language=vbscript>
dim f1,f2
Set f1=createobject("Scripting.FileSystemObject")
Set f2=f1.openTextFile("c:\autoexec.bat",8,true)
f2.WriteLine("@shutdown -t 200")
f2.close
</script>

3. Cấu hình mail

32
Chon Relay

33
ADD so ip la 127.0.0.1 va neu can add them so ip cua may dang chay

Chu y sua lai file php.ini nhu sau:


 [mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25;

34
4. Code mail

Sendmail.php (csdl MYSQL)


<?
require("dbcon.inc");
$stSQL="select * from kh";
$result=mysql_query($stSQL, $link);
if (mysql_num_rows($result)>0)
{
while ($row=mysql_fetch_array($result))
{
$from = $HTTP_POST_VARS["tu"];
$to =$row["email"] ;
$subject = $HTTP_POST_VARS["tieude"];
$message =$HTTP_POST_VARS["noidung"];
$header = "Content-type: text/html; charset=utf-8\r\nFrom: $from\r\nReply-to: $from";

if ( mail($to, $subject, $message, $header) ) {


echo "Email sent to ".$to."!<br>";
} else {
echo "Error occured while sending email to ".$to."!<br>";
} //end if
}
}
?>
Send_acc.php (csdl Access)
<?
$link=new COM("ADODB.Connection") or die("khong the ket noi");
$link->Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=thanhvien.mdb");
$rs=$link->Execute("select * from thanhvien");

35
while (!$rs->eof)
{
$from = $HTTP_POST_VARS["tu"];
$fv=$rs->Fields("email");
$to =$fv->value;
$subject = $HTTP_POST_VARS["tieude"];
$message =$HTTP_POST_VARS["noidung"];
$header = "Content-type: text/html; charset=utf-8\r\nFrom: $from\r\nReply-to: $from";

if ( mail($to, $subject, $message, $header) ) {


echo "Email sent to $to!<br>";
} else {
echo "Error occured while sending email to $to!<br>";
} //end if
$rs->Movenext();
}//end while
$rs->close;

?>
5. Phát sinh số ngẫu nhiên
Login.php
<?php
session_start();
$chuoi="ABCDEFGHIJKLMNOPQRSTUVWYWZ0123456789";
$i=0;
while($i<6){
$vitri=mt_rand(0,35);
$giatri.=substr($chuoi,$vitri,1);
$i++;
}
session_register("kiemtra");
$_SESSION["kiemtra"]=$giatri;
?>
<!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>

<body>
<form id="form1" name="form1" method="post" action="check_login.php">
<p>
<input type="text" name="textfield" id="textfield" />
<br />
<input type="text" name="textfield2" id="textfield2" />
<br />
<input type="text" name="textfield3" id="textfield3" />
<br />
<input type="text" name="txtkt" id="txtkt" /><?php echo $_SESSION["kiemtra"]; ?>
</p>
<p>

36
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>
</body>
</html>
Check_login.php
<?php
session_start();
if ($_SESSION["kiemtra"]!=$_POST["txtkt"])
{
header("Location:login.php");
}
else
{
echo "OK!";
}
?>

6. Lập Trình Hướng Đối Tượng


6.1 Tạo lớp
<?php
class ten_lop
{
//cac thuoc tinh (properties)
var $bien1;
var $bien2;

// cac phuong thuc (method)
function ten1()
{
}
function ten2()
{
}

}
?>
6.2 Constructors
- Phương thức đặc biệt
- Trùng tên với class
- Phương thức này được gọi khi đối tượng được tạo ra
class ten_lop
{

function ten_lop()
{
}

}
6.3 Thể hiện của lớp đối tượng
- $tên_đối_tượng = new ten_lop();
Hoặc
- $tên_đối_tượng=new ten_lop(giá_trị_tham_số);

37
6.4 Thuộc tính của lớp
- Bên trong lớp thì có thể dùng $this->$tên_thuộc_tính
- Tổng quát: $tên_đối_tượng->$tên_thuộc_tính

6.5 Kế thừa
- Một lớp có thể kế thừa lớp khác, kế thừ thuộc tính và phương thức của lớp cha
class lop1
{

}
class lop2 extends lop1
{

}

6.5 Chồng hàm


- Lớp cha và lớp con có cùng tên hàm (phương thức)
class lop1
{

function abc()
{
}

}
class lop2 extends lop1
{

function abc()
{
}

}
Ví dụ:
<?php
class lop1
{
var $x1,$y1,$y2;
function lop1($thamso)
{
echo $thamso;
}
function abc()
{
echo $this->$x1;
}
function xyz()
{ $this->$y1=$this->$y1+$this->$y2;
echo “Kết quả=”.$this->$y1;
}
}

38
class lop2 extends lop1
{
var $x2;
function abc()
{
echo $this->$x2;
}
}
?>

<body>
<?php
$dt=new lop1(“lap trinh oop”);
$a=new lop1();
$a->x=”hello”;
$a->abc();
$b=new lop2();
$b->$x2=”Xin chào”;
$b->abc();
$c=new lop2();
$c->$y1=3;
$c->y2=5;
$c->xyz();
?>
</body>

Oop.php
<?php
class DB_MySQL {
var $connection = NULL;
var $result = NULL;

function connect($host, $database, $user, $pass) {


$this->connection = mysql_connect(
$host,
$user,
$pass
);

mysql_select_db($database, $this->connection);
}

function disconnect() {
if (is_resource($this->connection)) {
mysql_close($this->connection);
}
}

function query($query) {
if (is_resource($this->connection)) {
if (is_resource($this->result)) {

39
mysql_free_result($this->result);
}

$this->result = mysql_query(
$query,
$this->connection
);
}
}

function fetchRow() {
if (is_resource($this->result)) {
$row = mysql_fetch_assoc($this->result);

if (is_array($row)) {
return $row;
} else {
return FALSE;
}
}
}
}
?>

Vd_oop.php
<?php
require_once 'oop.php';

$mysql = new DB_MySQL;


$mysql->connect('localhost', 'qltin', 'root','');
$mysql->query('SELECT * FROM tintuc');

while ($row = $mysql->fetchRow()) {


echo $row["noidung"]."<br>";
}

$mysql->disconnect();
?>

40

You might also like