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 of this site: "Getting Started with Yii Framework and Summary of Common Techniques", " Summary of PHP Excellent Development Framework", "Basic Tutorial for Getting Started with Smarty Templates", "Introduction Tutorial for PHP Object-Oriented Programming", "Summary of PHP String Usage", "Introduction Tutorial for PHP+MySQL Database Operation" and "PHP Common Databases" Summary of operating skills》
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.
The above has introduced the yii2 controller Controller Ajax operation example, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.