PHP+ajax no refresh to delete data
- 2020-03-31 20:26:42
- OfStack
First of all, this example is modified based on the message book.
We use jquery.js for ajax and dom deletion
First of all to join
Give the table a
Write a js:
Remove the link and change it to href="javascript:delItem('< ! -- {$item. Id} -- -- > ')"
The delete.php modification is to change the error statement to direct output.
OK to complete.
The index. The TPL:
Delete. PHP:
We use jquery.js for ajax and dom deletion
First of all to join
<script type="text/javascript" src="lib/jquery.js"></script>
Give the table a
id="t<!--{$item.id}-->"
Write a js:
<script>
function delItem (id) {
$.get('delete.php?id='+id,null,function (msg) {//An ajax request that executes the following code
if ('1'==msg) {//Returning 1 indicates success
$('#t'+id).remove();//Delete the table with id TXX
} else {//Otherwise an error message pops up
alert(msg);
}
});
}
</script>
Remove the link and change it to href="javascript:delItem('< ! -- {$item. Id} -- -- > ')"
The delete.php modification is to change the error statement to direct output.
OK to complete.
The index. The TPL:
<!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> All messages </title>
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
<script type="text/javascript" src="lib/jquery.js"></script>
</head>
<body>
<!--{if $smarty.session.username}-->
Welcome:<!--{$smarty.session.username}-->
<a href="logout.php"> exit </a>
<!--{else}-->
<a href="login.php"> The login </a>
<a href="reg.php"> registered </a>
<!--{/if}-->
<a href="add.php"> Published comments </a>
<!--{foreach from=$gblist item=item}-->
<table id="t<!--{$item.id}-->" width="700" border="0" cellspacing="0" cellpadding="0" class="tb">
<tr>
<td class="bg"><b>[<!--{$item.username}-->]</b> Published in: <!--{$item.insert_time}--></td>
</tr>
<tr>
<td><!--{$item.content}-->
<br />
<!--{if $item.user_file}-->
Attachment: <a target="_blank" href="uploads/<!--{$item.user_file}-->"><!--{$item.user_file}--></a>
<!--{/if}-->
</td>
</tr>
<tr>
<td align="right"><!--{if $item.user_id==$smarty.session.user_id}--><a href="add.php?id=<!--{$item.id}-->"> Modify the </a> <a href="javascript:delItem('<!--{$item.id}-->')"> delete </a><!--{/if}--></td>
</tr>
</table>
<!--{/foreach}-->
<!--{$pagePanel}-->
<script>
function delItem (id) {
$.get('delete.php?id='+id,null,function (msg) {
if ('1'==msg) {
$('#t'+id).remove();
} else {
alert(msg);
}
});
}
</script>
</body>
</html>
Delete. PHP:
<?php
require('common.php');
//Check out the message
$q = $query->query('select * from gb_content where id='.intval($_GET['id']));
$rs = $query->fetch_array($q);
$error = array();
if ($rs['user_id']!=intval($_SESSION['user_id'])) {//Determine whether the user_id is the same
$error = ' You can't delete this information, only delete your own post ';
}
if (!$error) {
$query->query('delete from gb_content where id='.intval($_GET['id']));//Delete statements
if ($rs['user_file']) {//Delete the attachment
@unlink('uploads/'.$rs['user_file']);
}
echo 1;//Indicates success
} else {
echo $error;
}
?>