Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- function myFunction()
- {
- $.ajax({
- url: '<?php echo Yii::$app->request->baseUrl. '/supermarkets/sample' ?>',
- type: 'post',
- data: {searchname: $("#searchname").val() , searchby:$("#searchby").val()},
- success: function (data) {
- alert(data);
- }
- });
- }
- </script>
- <?php
- use yiihelpersHtml;
- use yiiwidgetsLinkPager;
- ?>
- <h1>Supermarkets</h1>
- <ul>
- <select id="searchby">
- <option value="" disabled="disabled" selected="selected">Search by</option>
- <option value="Name">Name</option>
- <option value="Location">Location</option>
- </select>
- <input type="text" value ="" name="searchname", id="searchname">
- <button onclick="myFunction()">Search</button>
- <h3> </h3>
- public function actionSample(){
- echo "ok";
- }
- 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 = yiiwebResponse::FORMAT_JSON;
- return [
- 'search' => $search,
- 'code' => 100,
- ];
- }
- }
- $.ajax({
- url: '<?php echo Yii::$app->request->baseUrl. '/supermarkets/sample' ?>',
- type: 'post',
- data: {
- searchname: $("#searchname").val() ,
- searchby:$("#searchby").val() ,
- _csrf : '<?=Yii::$app->request->getCsrfToken()?>'
- },
- success: function (data) {
- console.log(data.search);
- }
- });
- _csrf: yii.getCsrfToken()
Add Comment
Please, Sign In to add comment