This time I will bring you a detailed explanation of the steps of Ajax deleting data and viewing data operations. What are the precautions for Ajax deleting data and viewing data operations? The following is a practical case, let's take a look.
1. Find a table in the database:

Color table
2. Main page
The code of the main page uses tbody;
The function of TBODY is:
can control the downloading of tables in separate rows, thereby increasing the download speed.
(The web page is opened after all the contents of the table are downloaded before it is displayed. For branch downloads, part of the content can be displayed first, which will reduce the user's waiting time.
The purpose of using TBODY It is possible to prevent these included codes from being displayed together after the entire table is parsed. That is to say, if there are multiple rows, then if you get a TBODY row, you can display one row first.
BODY is HTML. Text body, an HTML file, has only one BODY, and there can be multiple TBODYs in TABLE. The
TBODY tag can control the table to be downloaded in separate rows. It is more practical when the table content is large and needs to be downloaded in separate rows. Add and,
For example:
The following is the quoted content: head1head2 is displayed first, then displayed, and then foot1foot2
Note:
*1. The TBODY element will not be rendered in the browser
*2. When merging cells between different rows, do not add the TBODY tag to the row where each cell is located
Tips: The valid tags contained in the TBODY element are: TD, TH, TR. Special reminder: The running effect of this example code will not be seen because the content in the table is relatively small.
Only when the amount of data is large And the effect can only be seen when there are more nested tables.
Main page code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <!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>无标题文档</title>
<script src= "jquery-1.11.2.min.js" ></script>
</head>
<body>
<h1>显示数据</h1>
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td>代号</td>
<td>名称</td>
<td>操作</td>
</tr>
<tbody id= "td" >
</tbody>
</table>
</body>
</html>
<script>
$.ajax({
url: "jiazai.php" ,
dataType: "TEXT" ,
success: function (data)
{
}
});
</script>
|
Copy after login
Picture:

##Callback function. It is empty, come back and write it later;
Then comes the loading page:
Display:
Traverse the array and display the contents of the table, specifically:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php
include ( "db.class.php" );
$db = new db();
$sql = "select * from min" ;
$arr = $db ->Query( $sql );
$str = "" ;
foreach ( $arr as $v )
{
$str = $str .implode( "-" , $v ). "|" ;
}
echo $str ;
|
Copy after login
Let’s take a look at the output:

There is an extra vertical line at the end, remove the vertical line:
1 2 3 | $str = substr ( $str ,0, strlen ( $str )-1);
|
Copy after login
Come again Look:

Now let’s write the callback function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <script>
$.ajax({
url: "jiazai.php" ,
dataType: "TEXT" ,
success: function (data)
{
var str = "" ;
var hang = data.split( "|" );
for ( var i = 0;i<hang.length;i++)
{
var lie = hang[i].split( "-" );
str = str+
"<tr><td>"
+lie[0]+
"</td><td>"
+lie[1]+
"</td><td>操作</td></tr>" ;
}
$( "#td" ).html(str);
}
});
</script>
|
Copy after login
After writing, look at the next page:

3. Next you can write delete:
First add a delete button in the last cell and pass a primary key value:
1 2 3 4 | "</td><td>" +
"<input type='button' ids='" +lie[0]+ "' class='sc' value='删除' />" +
"</td></tr>" ;
|
Copy after login

Add an event to the delete button and call the Ajax method:
**
The difference between asynchronous and synchronous:
Synchronization needs to wait for the return result before continuing. Asynchronous does not need to wait. Generally, you need to monitor the asynchronous results.
Synchronization is a queue in a straight line, and asynchronous is not in a queue.
**
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$( ".sc" ).click( function (){
var ids = $(this).attr( "ids" );
$.ajax({
url: "shanchu.php" ,
data:{ids:ids},
dataType: "TEXT" ,
type: "POST" ,
success: function (d) {
}
});
})
|
Copy after login
The callback function waits and comes back to write;
Continue to delete the processing page:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
include ( "db.class.php" );
$db = new db();
$ids = $_POST [ "ids" ];
$sql = "delete from min WHERE ids='{$ids}'" ;
if ( $db ->Query( $sql ,0))
{
echo "ok" ;
}
else {
echo "no" ;
}
|
Copy after login
Copy after login
Look at it this way:
Click to delete, the page will not be refreshed after deletion,
若是让他自动加载数据,需要把加载数据的代码封装成一个方法,删除的时候调用此方法;就哦可了
主页面代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | <!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>无标题文档</title>
<script src= "jquery-1.11.2.min.js" ></script>
</head>
<body>
<h1>显示数据</h1>
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td>代号</td>
<td>名称</td>
<td>操作</td>
</tr>
<tbody id= "td" >
</tbody>
</table>
</body>
</html>
<script>
load();
function load()
{
$.ajax({
url: "jiazai.php" ,
dataType: "TEXT" ,
success: function (data) {
var str = "" ;
var hang = data.split( "|" );
for ( var i = 0; i < hang.length; i++) {
var lie = hang[i].split( "-" );
str = str +
"<tr><td>"
+ lie[0] +
"</td><td>"
+ lie[1] +
"</td><td>" +
"<input type='button' ids='" + lie[0] + "' class='sc' value='删除' />" +
"</td></tr>" ;
}
$( "#td" ).html(str);
$( ".sc" ).click( function () {
var ids = $(this).attr( "ids" );
$.ajax({
url: "shanchu.php" ,
data: {ids: ids},
dataType: "TEXT" ,
type: "POST" ,
success: function (d) {
if (d.trim() == "ok" ) {
alert( "删除成功" );
load();
}
else {
alert( "删除失败" );
}
}
});
})
}
});
}
</script>
|
Copy after login
删除页面代码:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
include ( "db.class.php" );
$db = new db();
$ids = $_POST [ "ids" ];
$sql = "delete from min WHERE ids='{$ids}'" ;
if ( $db ->Query( $sql ,0))
{
echo "ok" ;
}
else {
echo "no" ;
}
|
Copy after login
Copy after login
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
Ajax不刷新页面的情况下实现分页查询
ajax分页查询图文详解
The above is the detailed content of Detailed explanation of the steps of Ajax deleting data and viewing data operations. For more information, please follow other related articles on the PHP Chinese website!