The example in this paper describes the method of YII2 controller controllers ' Ajax operation. Share to everyone for your reference, as follows:
Public Function Actionsample () {if (Yii:: $app->request->isajax) { $data = Yii:: $app->request->post (); $searchname = Explode (":", $data [' searchname ']); $searchby = Explode (":", $data [' Searchby ']); $searchname = $searchname [0]; $searchby = $searchby [0]; $search =//your logic; \yii:: $app->response->format = \yii\web\response::format_json; return [ ' search ' = $search, ' Code ' = [ ];}}
Template Invocation Method
$.ajax ({ URL: ' <?php echo Yii:: $app->request->baseurl. '/supermarkets/sample '?> ', type: ' Post ', data: {searchname: $ ("#searchname"). Val (), searchby:$ ("# Searchby "). Val ()}, success:function (data) { console.log (data.search); });
For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
It is hoped that this article is helpful to the PHP program design based on YII framework.