这是不刷新页面删除记录的完整源代码。
按照步骤:
步骤1:
DBConnect.php
class DBConnect
{
function DBConnect()
{
$link= mysql_connect("localhost","root","")or die("Local Host Error".mysql_error());
mysql_select_db("test");
}
function viewData()
{
$query = "SELECT * FROM test_mysql";
$resultset = mysql_query($query);
return $resultset;
}
function DeleteData($userID)
{
$query = "DELETE FROM test_mysql WHERE id=".$userID;
$resultset=mysql_query($query) ;
}
}
第2步:
delete.php
include_once'DBConnect.php';
if(isset($_REQUEST['userid']) && $_REQUEST['userid'])
{
$delObj= new DBConnect();
$delObj->DeleteData($_REQUEST['userid']);
}
第 3 步:
index.php
<html>
<head>
<title></title>
<script type="text/javascript">
function deletedata(id)
{
var xmlhttp;
if (id=="")
{
document.getElementById("Display").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
window.location.reload()
}
}
xmlhttp.open("GET","delete.php?userid="+id,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
include 'DBConnect.php';
$ViewObj= new DBConnect();
$ResultSet=$ViewObj->viewData();
?>
<span id ="Display">
<table align="center" border="1" width="50%" cellpadding="4" cellspacing="4">
<tr>
<th>ID</th>
<th>Name</th>
<th>operation</th>
<th align="center">Action</th>
</tr>
<?php
while($row= mysql_fetch_array($ResultSet))
{
?>
<tr>
<td><?php echo $row[0];?></td>
<td><input type="text" name="txt"></td>
<td><?php echo $row[1];?></td>
<td align="center"><a href="#" onClick="deletedata('<?php echo $row[0];?>')" style="color:#00F"><b>Delete</b></a></td>
</tr>
<?php
}
?>
</table>
</span>
</body>
</html>
如果您觉得有任何问题,请告诉我。希望对您有所帮助。谢谢你。