Guest User

Untitled

a guest
Oct 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. <script>
  2. function myFunction()
  3. {
  4. $.ajax({
  5. url: '<?php echo Yii::$app->request->baseUrl. '/supermarkets/sample' ?>',
  6. type: 'post',
  7. data: {searchname: $("#searchname").val() , searchby:$("#searchby").val()},
  8. success: function (data) {
  9. alert(data);
  10.  
  11. }
  12.  
  13. });
  14. }
  15. </script>
  16.  
  17. <?php
  18. use yiihelpersHtml;
  19. use yiiwidgetsLinkPager;
  20.  
  21. ?>
  22. <h1>Supermarkets</h1>
  23. <ul>
  24.  
  25. <select id="searchby">
  26. <option value="" disabled="disabled" selected="selected">Search by</option>
  27. <option value="Name">Name</option>
  28. <option value="Location">Location</option>
  29. </select>
  30.  
  31. <input type="text" value ="" name="searchname", id="searchname">
  32. <button onclick="myFunction()">Search</button>
  33. <h3> </h3>
  34.  
  35. public function actionSample(){
  36. echo "ok";
  37. }
  38.  
  39. public function actionSample()
  40. {
  41. if (Yii::$app->request->isAjax) {
  42. $data = Yii::$app->request->post();
  43. $searchname= explode(":", $data['searchname']);
  44. $searchby= explode(":", $data['searchby']);
  45. $searchname= $searchname[0];
  46. $searchby= $searchby[0];
  47. $search = // your logic;
  48. Yii::$app->response->format = yiiwebResponse::FORMAT_JSON;
  49. return [
  50. 'search' => $search,
  51. 'code' => 100,
  52. ];
  53. }
  54. }
  55.  
  56. $.ajax({
  57. url: '<?php echo Yii::$app->request->baseUrl. '/supermarkets/sample' ?>',
  58. type: 'post',
  59. data: {
  60. searchname: $("#searchname").val() ,
  61. searchby:$("#searchby").val() ,
  62. _csrf : '<?=Yii::$app->request->getCsrfToken()?>'
  63. },
  64. success: function (data) {
  65. console.log(data.search);
  66. }
  67. });
  68.  
  69. _csrf: yii.getCsrfToken()
Add Comment
Please, Sign In to add comment