thinkphp Ajax and PHP response Process _php Example

Source: Internet
Author: User

In this paper, we analyze the Ajax and PHP response process in thinkphp. Share to everyone for your reference. The specific analysis is as follows:

Generally will be the front page search results, do not like the content (link), delete, because the entire site's programming framework-type thinkphp, the use of JS Ajax in response to the page, call the back-end PHP interface, the implementation of the foreground and the background of the database update.

The first thing we need to do is to add a text "delete" to the foreground page to add:

Copy Code code as follows:
<a href= "javascript:void (0);" id= <php>echo $val [id]</php> "class=" delete "> Delete </a>

The above HTML code means: add an href to delete, this href is a JS function, similar to the role of hyperlinks, of course, for my novice at the beginning of the time will be puzzled, our usual hyperlinks are not such, the Web page a lot of javascript:void (0) , how does the program know that the user clicks on the deletion, will respond to its real corresponding JS function? Don't worry, that's why you add tags such as ID and class for deletion, and I believe that when I post the code in JS, you will understand that the code is as follows:

Copy Code code 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 response to the user clicks "Location" in the page, the added event is click (), Add the code in the click to respond to the event: var id = jQuery (_this). attr ("id"), get a corresponding ID, because this ID is the link ID we used to delete the database, of course, this direct plaintext is not a good way, here just the entire response process. Invoke Ajax to implement asynchronous running of the entire process. We have set JS to accept the JSON string, there should be a variety of ways, I did not go to try, if you are interested can try it on their own. /search/index.php/jason/delete? This phrase is JS to request PHP interface, PHP interface to use the following code:

Note that Ajax's path to the PHP interface, that is, the delete function must be placed in the JasonAction.class.php, this example must be:

Copy 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 are not unfamiliar with the above code, the whole process is thinkphp framework internal configuration completed.

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 Code code as follows:
if (del = = 1) {//delete succeeded
JQuery ("#" +id). Parents (". List"). Remove ();//This sentence is after the successful background deletion, directly in the foreground of the response to the DIV to delete, so that performance will be very fast, no need for background data to be extracted to the foreground page,
}else{//Delete failed
Alert ("Delete failed");
}

The whole response process is like this, as for effect optimization, is the optimization and refinement aspects of the problem, later, colleagues around me and told me to remove the response link another dynamic effect:

Upcoming

Copy Code code as follows:
JQuery ("#" +id). Parents (". List"). Remove ();
To be replaced by:
Copy Code code as follows:
JQuery ("#" +id). Parents (". List"). Slideup ("Slow", function () {
JQuery (This). Remove ();
});

You may wish to try the effect, very good, well, the response process is recorded, only their own personal insights and perceptions.

I hope this article will help you with the PHP program design based on thinkphp framework.

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.