1. <legend id='snyKk'><style id='snyKk'><dir id='snyKk'><q id='snyKk'></q></dir></style></legend>
        <bdo id='snyKk'></bdo><ul id='snyKk'></ul>
      <i id='snyKk'><tr id='snyKk'><dt id='snyKk'><q id='snyKk'><span id='snyKk'><b id='snyKk'><form id='snyKk'><ins id='snyKk'></ins><ul id='snyKk'></ul><sub id='snyKk'></sub></form><legend id='snyKk'></legend><bdo id='snyKk'><pre id='snyKk'><center id='snyKk'></center></pre></bdo></b><th id='snyKk'></th></span></q></dt></tr></i><div id='snyKk'><tfoot id='snyKk'></tfoot><dl id='snyKk'><fieldset id='snyKk'></fieldset></dl></div>

      <small id='snyKk'></small><noframes id='snyKk'>

      <tfoot id='snyKk'></tfoot>

        Yii2 中的 Ajax + 控制器动作

        时间:2023-10-16
      1. <legend id='ulkG3'><style id='ulkG3'><dir id='ulkG3'><q id='ulkG3'></q></dir></style></legend>

          <tfoot id='ulkG3'></tfoot>

              <bdo id='ulkG3'></bdo><ul id='ulkG3'></ul>
            • <small id='ulkG3'></small><noframes id='ulkG3'>

                <tbody id='ulkG3'></tbody>

                  <i id='ulkG3'><tr id='ulkG3'><dt id='ulkG3'><q id='ulkG3'><span id='ulkG3'><b id='ulkG3'><form id='ulkG3'><ins id='ulkG3'></ins><ul id='ulkG3'></ul><sub id='ulkG3'></sub></form><legend id='ulkG3'></legend><bdo id='ulkG3'><pre id='ulkG3'><center id='ulkG3'></center></pre></bdo></b><th id='ulkG3'></th></span></q></dt></tr></i><div id='ulkG3'><tfoot id='ulkG3'></tfoot><dl id='ulkG3'><fieldset id='ulkG3'></fieldset></dl></div>
                • 本文介绍了Yii2 中的 Ajax + 控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是编程新手,我试图在用户输入数据并单击提交按钮时调用一个函数.我正在使用 Yii2 并且我不熟悉 Ajax.我尝试开发一个函数,但没有调用我的控制器操作.

                  I'm new to programming, and I'm trying to call a function when the user inputs data and clicks submit button. I'm using Yii2 and I'm not familiar with Ajax. I tried developing a function, but my controller action isn't called.

                  这是我正在尝试的示例代码:

                  Here is the example code I'm trying:

                  views/index.php:

                  views/index.php:

                  <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";
                  }
                  

                  我的问题是,当我单击搜索"按钮时什么也没有发生,当我尝试调试它时,调试器不运行任何代码!

                  My problem is that when I click on the Search button nothing happens, and when I try to debug it, the debugger runs no code!

                  推荐答案

                  这是您可以根据需要修改的示例

                  This is sample you can modify according your need

                  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 成功块中获得数据.查看浏览器控制台.

                  If this will success you will get data in Ajax success block. See browser console.

                    $.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);
                         }
                    });
                  

                  这篇关于Yii2 中的 Ajax + 控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将分钟添加到日期时间无法正常工作 下一篇:尝试使用带有 Yii2 接收类的 PHP DateTime 类未找到错误

                  相关文章

                  <small id='I77eJ'></small><noframes id='I77eJ'>

                  <tfoot id='I77eJ'></tfoot>

                    <bdo id='I77eJ'></bdo><ul id='I77eJ'></ul>
                  <legend id='I77eJ'><style id='I77eJ'><dir id='I77eJ'><q id='I77eJ'></q></dir></style></legend>

                • <i id='I77eJ'><tr id='I77eJ'><dt id='I77eJ'><q id='I77eJ'><span id='I77eJ'><b id='I77eJ'><form id='I77eJ'><ins id='I77eJ'></ins><ul id='I77eJ'></ul><sub id='I77eJ'></sub></form><legend id='I77eJ'></legend><bdo id='I77eJ'><pre id='I77eJ'><center id='I77eJ'></center></pre></bdo></b><th id='I77eJ'></th></span></q></dt></tr></i><div id='I77eJ'><tfoot id='I77eJ'></tfoot><dl id='I77eJ'><fieldset id='I77eJ'></fieldset></dl></div>