Home > Backend Development > PHP Tutorial > yii2 controller Controller Ajax operation example_php example

yii2 controller Controller Ajax operation example_php example

WBOY
Release: 2016-08-04 08:56:48
Original
974 people have browsed it

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,
  ];
 }
}

Copy after login

Template calling method

$.ajax({
    url: '<&#63;php echo Yii::$app->request->baseUrl. '/supermarkets/sample' &#63;>',
    type: 'post',
    data: {searchname: $("#searchname").val() , searchby:$("#searchby").val()},
    success: function (data) {
     console.log(data.search);
    }
});

Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template