PHP + ajax: refreshing data deletion

Source: Internet
Author: User

First, this example is modified based on the message collation.
We used jquery. js to delete ajax and dom.
Join
Copy codeThe Code is as follows:
<Script type = "text/javascript" src = "lib/jquery. js"> </script>

Add a table
Copy codeThe Code is as follows:
Id = "t <! -- {$ Item. id} -->"

Write a js file:
Copy codeThe Code is as follows:
<Script>
Function delItem (id ){
$. Get ('delete. php? Id = '+ id, null, function (msg) {// ajax request. After the request, run the following code:
If ('1' = msg) {// 1 indicates success
$ ('# T' + id). remove (); // delete a table whose id is txx
} Else {// otherwise, an error message is displayed.
Alert (msg );
}
});
}
</Script>

Change the link to href = "javascript: delItem ('<! -- {$ Item. id} --> ')"
The modification to delete. php is to change the error statement to direct output.
OK.
Index. tpl:
Copy codeThe Code is as follows:
<! 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"> logon </a>
<A href = "reg. php"> register </a>
<! -- {/If} -->
<A href = "add. php"> post a message </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> posted on: <! -- {$ 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 </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:
Copy codeThe Code is as follows:
<? Php
Require ('common. php ');
// Query 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']) {// judge whether the user_id is the same
$ Error = 'you cannot delete this information. You can only delete your own release ';
}
If (! $ Error ){
$ Query-> query ('delete from gb_content where id = '. intval ($ _ GET ['id']); // delete a statement
If ($ rs ['user _ file']) {// delete an attachment
@ Unlink ('uploads/'. $ rs ['user _ file']);
}
Echo 1; // success
} Else {
Echo $ error;
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.