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 + 控制器动作

        Ajax + Controller Action in Yii2(Yii2 中的 Ajax + 控制器动作)
      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 + 控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  <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>
                        <tbody id='I77eJ'></tbody>