Ajax页面无刷新
先建一个Html的文件delete.html如下:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" charset="utf-8" />
<meta name="author" content="blog.anchen8.net" />
<title>Ajax</title>
</head>
<body>
<center>
<form>
<h1>会员信息</h1>
<hr />
<br/>
<div id="done"></div>
<input type="button" id="dl" value="获取会员表"/>
</form>
</center>
</body>
</html>
<script>
var xmlHttp;
function CreateHTTP() {
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
//把相同的代码封装下
function Common() {
CreateHTTP();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
//接受数据--》一张表的字符串
var table = xmlHttp.responseText;
//显示在网页中
document.getElementById("done").innerHTML = table;
}
}
}
document.getElementById("dl").onclick = function () {
Common();
xmlHttp.open("get", "leaguerDelete.php");
xmlHttp.send();
}
//删除链接的脚本
function dd(id) { //id传过来的删除的ID
Common();
xmlHttp.open("get", "leaguerDelete.php?id=" + id);
xmlHttp.send()
}
</script>
再建一个php文件leaguerDelete.php 如下:
<?php
$conn=mysql_connect("localhost","root","19960621");
mysql_select_db("dbleaguer",$conn);
function Demo(){
$rlt=mysql_query("select * from tbleaguer");
$nt="";
while($row=mysql_fetch_array($rlt)){
$nt.="<tr style='color:Black;background-color:#DEDFDE;'>
<td>".$row["LID"]."</td><td>".$row["Name"]."</td><td>".$row["Pwd"]."</td><td>".$row["Birth"]."</td><td>".$row["Sex"]."</td><td>".$row["Tel"]."</td><td>".$row["Mail"]."</td><td>".$row["Grade"]."</td><th><a href='javascript:dd(".$row["LID"].")'>删除</a></th>
</tr>";
}
$newTable="<table cellspacing='1' cellpadding='3' id='GridView1' style='background-color:White;border-color:White;border-width:2px;border-style:Ridge;'>
<tr style='color:#E7E7FF;background-color:#4A3C8C;font-weight:bold;'>
<th scope='col'>序号</th><th scope='col'>用户</th><th scope='col'>密码</th><th scope='col'>出生日期</th><th scope='col'>性别</th><th scope='col'>联系方式</th><th scope='col'>邮箱</th><th scope='col'>等级</th><th scope='col'>操作</th>
</tr>".$nt.
$newTable."</table>";
echo $newTable;
}
if($_GET['id']!=null){
$a=strval($_GET['id']);
mysql_query("delete from tbleaguer where LID=".$a);
Demo();
}
else{
Demo();
}
?>

浙公网安备 33010602011771号