Detailed explanation of the response process between ajax and php in thinkphp, thinkphpajax_PHP tutorial

Source: Internet
Author: User
In thinkphp, the ajax and php response processes are described in detail, thinkphpajax. The response process of ajax and php in thinkphp is described in detail. this document analyzes the response process of ajax and php in thinkphp. Share it with you for your reference. The specific analysis is as follows: Generally, the ajax and php response processes in thinkphp are described in detail, thinkphpajax

This article analyzes the ajax and php response processes in thinkphp. Share it with you for your reference. The specific analysis is as follows:

In general, the content (link) that you do not like in the front-end page search results is deleted, because the programming framework thinkphp of the entire website uses ajax in js to respond to the page, call the backend php interface to update both the front-end and back-end databases.

The first thing we need to do is to add a text "delete" to the foreground page, which can be added as follows:

The code is as follows:

Echo $ val [id]"Class =" delete "> delete

The above html code means to add an href to the deletion. this href is a js function, which is similar to the function of a hyperlink. of course, it may be confusing for a newbie like me at the beginning, our usual hyperlinks are not like this. There are a lot of javascript: void (0) in the webpage. how does the program know that the user will respond to the js function after Clicking delete? Don't worry, that's why the tags such as id and class will be deleted. I believe you will understand the code after I publish the code in js. the code is as follows:

The code is as follows:

JQuery (". list a. delete"). click (function (){
If (confirm ("Are you sure you want to delete it? ")){
Var _ this = this;
Var id = jQuery (_ this). attr ("id ");
JQuery. ajax ({
Url: '/Search/index. php/Jason/delete? ',
Data: {'id': id },
DataType: 'json ',
Success: function (data ){
Var del = data. del;
If (del = 1) {// deletion successful
JQuery ("#" + id). parents (". list"). remove ();
} Else {// deletion failed
Alert ("deletion failed ");
}
}
});
}
});


Look at the function header: jQuery (". list. delete "). click (), the label behind jQuery is very important, which ensures that jquery is very accurate in response to the "location" clicked by the user in the page, the added event is click (), add the event response code in click: var id = jQuery (_ this ). attr ("id"); get the id corresponding to a, because this id is the link id used when we delete the database, of course, this direct plaintext method is not good, here we will only introduce the entire response process. Call ajax to implement asynchronous operation. We set js to accept json strings. There are many methods here. I didn't try it. if you are interested, you can try it on your own. /Search/index. php/Jason/delete? This is the php interface requested by js. the code used for the php interface is as follows:

Note that the path for ajax to evaluate the php interface, that is, the delete function must be placed in JasonAction. class. php. this example must be as follows:

The code is as follows:

Public function delete (){
If ($ this-> isGet ()){
$ UserId = session ("uid"); // user logon
If (! Empty ($ userId )){
$ A = M ('***');
$ Id = $ this-> _ get ('id ');
$ Result = $ a-> where ("id = $ id")-> delete (); // delete
If ($ result> 0 ){
$ Arr = array ("del" => '1 ');
} Else {
$ Arr = array ("del" => '0 ');
}
$ Json_str = json_encode ($ arr );
Echo $ json_str; // Return to js
}
}
}


I believe you will not be unfamiliar with the above code. The entire process is completed within the thinkphp framework.

Var del = data. del; this sentence is to accept the value of the del key in the json string returned by the php code, and then update and respond to the page at the front end.

The code is as follows:

If (del = 1) {// deletion successful
JQuery ("#" + id ). parents (". list "). remove (); // after the background is deleted successfully, the p in the response is deleted directly at the foreground, so that the performance will be fast, you do not need to reextract background data to the front-end page,
} Else {// deletion failed
Alert ("deletion failed ");
}


The entire response process is like this. performance optimization is a matter of optimization and refinement. later, my colleagues told me another dynamic effect of removing the response link at the front-end:

Coming soon:

The code is as follows:

JQuery ("#" + id). parents (". list"). remove ();

Replace:

The code is as follows:

JQuery ("#" + id). parents (". list"). slideUp ("slow", function (){
JQuery (this). remove ();
});


You may wish to try the results. it is very good. well, the response process is recorded, just your own personal opinions and cognition.

I hope this article will help you design PHP programs based on the ThinkPHP framework.

In this article, we analyze the ajax and php response processes in thinkphp. Share it with you for your reference. The specific analysis is as follows...

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.