thinkphp Ajax and PHP response process in detail, Thinkphpajax
This paper analyzes the Ajax and PHP response process in thinkphp. Share to everyone for your reference. The specific analysis is as follows:
Generally will be the foreground page search results, do not like the content (link), deleted, because the entire site programming framework thinkphp, the use of JavaScript in the AJAX response to the page, call the background PHP interface, to achieve the foreground and background database update.
The first thing we need to do is to add a text "delete" to the foreground page, so add:
Copy the code as follows: Echo $val [id] "class=" Delete > Delete
Above the meaning of the HTML code: is to delete add an href, this href is a JS function, similar to the role of hyperlinks, of course, for beginners like me at the beginning of the confusion, our usual hyperlink is not the case, the page is a lot of javascript:void (0) , how does the program know when the user clicks Delete, will respond to its true corresponding JS function? Don't worry, this is why you will add the ID and class tag for the deletion, believe that I put the code in JS after you will understand, the code is as follows:
Copy CodeThe 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) {//delete succeeded
JQuery ("#" +id). Parents (". List"). Remove ();
}else{//Delete failed
Alert ("Delete failed");
}
}
});
}
});
Everyone looks at the head of the function: jquery (". List A.delete"). Click (), the tag behind jquery is important, which ensures that jquery is very accurate in responding to the "location" of the user click on the page, adding the event as click (), Add the code that responds to the event in the click: var id = jQuery (_this). attr ("id"), get the ID of a corresponding, because this ID is the link ID that we used when we deleted the database, of course, this kind of direct clear way is not good, here just introduce the whole response process. Call Ajax to implement an asynchronous run of the entire process. We set the JS to accept the JSON string, there should be a lot of ways, I did not try, if you are interested to try it yourself. /search/index.php/jason/delete? This sentence is JS to request PHP interface, PHP interface to use the code as follows:
Note that Ajax is the path to the PHP interface, that is, the delete function must be placed in the JasonAction.class.php, this example must be:
Copy the Code Code as follows: Public function Delete () {
if ($this->isget ()) {
$userId = Session ("UID");//User Login
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;//back to JS
}
}
}
I believe you will not be unfamiliar with the above code, the whole process is thinkphp framework internal configuration is complete.
var del = Data.del, which accepts the value of the DEL key in the JSON string returned by the PHP code, and then updates and responds to the page in the foreground.
Copy the code as follows: if (del = = 1) {//delete succeeded
JQuery ("#" +id). Parents (". List"). Remove ();//This is the background to delete the successful, directly in the foreground of the response to the DIV to delete, so the performance will be fast, no need to re-extract background data to the foreground page,
}else{//Delete failed
Alert ("Delete failed");
}
The whole response process is this, as for the optimization of the effect, is the optimization and refinement of the problem, later, colleagues around me told me the foreground removes another dynamic effect of the response link:
Coming: Copy Code code as follows: JQuery ("#" +id). Parents (". List"). Remove (); Replace with:
Copy the code as follows: JQuery ("#" +id). Parents (". List"). Slideup ("Slow", function () {
JQuery (This). Remove ();
});
We may as well try the effect, very good, good, the response process is documented, but their own personal insights and cognition.
It is hoped that this article is helpful to the PHP program design based on thinkphp framework.
http://www.bkjia.com/PHPjc/923905.html www.bkjia.com true http://www.bkjia.com/PHPjc/923905.html techarticle thinkphp in the Ajax and PHP response process in detail, thinkphpajax this article to analyze the thinkphp Ajax and PHP response process. Share to everyone for your reference. The detailed analysis is as follows: General will front desk ...