Thinkphp Framework "ajax Method return" Example: Simply add a piece of data and query the data in a table

Source: Internet
Author: User

Note: thinkphp uses Ajax in the same way that Ajax is used before, except that the URLs in the previous Ajax point to a page, and the URLs inside the thinkphp need to point to an action Method.

Create a AjaxController.class.php in the Module Controllers Controller folder
<?phpnamespace admin\controller; usethink\controller;classAjaxcontrollerextendscontroller{ public functionAjax () {$this->show ();//Show Page    }     public functionajaxchuli () {the URL in the //ajax page points to this method $n= D ("Nation"); $attr=$n-Select (); $this->ajaxreturn ($attr);//Ajax return method Ajaxreturn ();    }         public functionAddajax () {$this-Show (); }     public functionaddchuli () {the URL in the //addajax page points to this method $n= D ("Nation"); $n->create ();//Automatic collection of forms: As long as the content page uses the Post value create () method is very convenient!         $r=$n->add ();//Add () method adds data        if($r){                        $this->ajaxreturn ("ok", "eval");//use the text value to add the Exal output string Data. }Else{            $this->ajaxreturn ("on", "eval"); }    }    }

In the module view (show page) View folder first create a good Ajax folder (for the Ajax controller), and then create a ajax.html page (representing your custom METHOD)

            $.Ajax ({url: "__controller__/ajaxchuli",//the point here is a method. data:{},DataType: "JSON",type: "POST",Success:function(data) {//Alert (data[0].code);//the Index is used here to not read the data using the EQ.                     varstr = "";  for(a in Data) {str= str+ "<option value= '" +data[a].code+ ">" +data[a].name+ "</option>"                    }                    $("#nation").HTML (str);        }                            }); </script> </body> 

In the module view (show page) View folder first create a good Ajax folder (for the Ajax controller), and then create a addajax.html page (representing your custom METHOD)

                    $("#btn"). Click (function(){                varCode = $ ("#code").Val (); varName = $ ("#name").Val (); $.Ajax ({url: "__controller__/addchuli",Data: {code:code,name:name},DataType: "TEXT",type: "POST",Success:function(data) {alert (data);            }                                });        }); </script> </body> 

Thinkphp Framework "ajax Method return" Example: Simply add a piece of data and query the data in a table

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.