Home >Backend Development >PHP Tutorial >yii2 controller Controller Ajax operation example_php example
The example in this article describes the method of Controller Ajax operation of yii2 controller. Share it with everyone for your reference, the details are 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' => 100, ]; } }
Template calling 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); } });
Readers who are interested in more Yii-related content can check out the special topics on this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent PHP Development Framework", "Basic Tutorial for Getting Started with Smarty Templates", "Introduction to PHP Object-Oriented Programming" Tutorial", "php string usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.