Home > Backend Development > PHP Tutorial > PHP+ajax delete data without refreshing_PHP tutorial

PHP+ajax delete data without refreshing_PHP tutorial

WBOY
Release: 2016-07-21 15:41:07
Original
846 people have browsed it

First, this example is modified based on the edited version of the guestbook.
We used jquery.js to implement ajax and dom deletion
First add

Copy the code The code is as follows:



Add
to the table Copy code The code is as follows:

id="t"

Write a js:
Copy the code The code is as follows:

<script> <br> function delItem (id) { <br>$.get('delete.php?id='+id,null,function (msg) {//ajax request, execute the following code after the request<br>if ('1'= =msg) {//Return 1 to indicate success<br>$('#t'+id).remove();//Delete the table with id txx<br>} else {//Otherwise an error message will pop up<br>alert(msg); <br>} <br>}); <br>} <br></script>

Delete the link and change it to href="javascript:delItem('< ;!--{$item.id}-->')"
The modification of delete.php is to change the error statement to output directly.
OK done.
index.tpl:
Copy code The code is as follows:





All comments



< !--{if $smarty.session.username}-->
Welcome:
Exit

Login
< a href="reg.php">Register

Post a message< ;/a>













<script> <br>function delItem (id) { <br>$.get('delete.php?id='+id,null,function (msg) { <br>if ('1'==msg) { <br>$('#t'+id). remove(); <br>} else { <br>alert(msg); <br>} <br>}); <br>} <br></script>



delete.php :
Copy code The code is as follows:

require('common.php');
// Query message information
$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 cannot delete this information, you can only delete what you posted';
}
if (!$error) {
$query->query('delete from gb_content where id='.intval($_GET['id']));//Delete statement
if ( $rs['user_file']) {//Delete attachment
@unlink('uploads/'.$rs['user_file']);
}
echo 1;//Indicates success
} else {
echo $error;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321249.htmlTechArticleFirst of all, this example is modified based on the edited version of the guestbook. We used jquery.js to implement ajax and dom deletion. First add the copy code. The code is as follows: script type="text/javascript" src="lib/jquery...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
< b>[] Posted by:




Attachment: < !--{$item.user_file}-->

Modify Delete