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