1

我正在使用 PrestaShop 付款方式,我有一个 tpl 页面,其中包含一个按钮和一个隐藏的 div。在按钮单击时,我想执行一个 ajax 函数并在成功时显示 div,但它什么也没做。

HTML 元素:

<input id="showVerifCode" type="button" value="Get Verification Code" style="background: #3e64ff; border-color: #3e64ff; color: #fff; padding: 11px 16px; cursor: pointer; border-width: 1px; border-radius: 2px; font-size: 14px; font-weight: 400; margin-top: 16px; margin-bottom: 0px;">

  <div id="verifCode" style="display: none;">
    <p style="margin-top: 5px;"><small id="email-send-info" style="color: #6fdc6f;">Please check your mail for the verification code!</small></p>
    <P style="margin-bottom: 0; margin-top: 16px;">Verification Code:</P>
    <input placeholder="E.g 1234567890" width="50px" name="verifCode" required type="text" style="margin-top: 1px; width: 100%; padding: 12px 20px; display: inline-block; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box;"/>
  </div>

JavaScript 函数:

{literal}
    <script type="text/javascript">
      const targetDiv = document.getElementById("verifCode");
      const btn = document.getElementById("showVerifCode");
      btn.onclick = function () {
        
        $.ajax({
          url: {$link->getModuleLink('prestashopbeam', 'AjaxSendMail.php', array())},
          type: 'GET',
          data: 'ajax=true&clientgroups='+ 'test5858',
          cache: false,
          success: function(data) {
            if (targetDiv.style.display === "none") {
              targetDiv.style.display = "block";
            }
          }
        });

      };
    </script>
  {/literal}

PHP Ajax 页面(位于 /modules/my_module/AjaxSendMail.php

<?php
return;

我在那里只有这个回报,因为我想先测试 ajax 部分。

抱歉,如果这是一个非常菜鸟的问题,但这是我第一次使用 ajax,而且我也不是 PHP 或 JavaScript 大师。

先感谢您。

编辑:我尝试过使用 JQuery,但结果是一样的。我确保代码在按下按钮时运行,仅让它显示隐藏的 div 并且它可以工作,但在我放置 ajax 代码时不会。

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  <script>
    {literal}
    $( "#showVerifCode" ).click(

        function() {

          $.ajax({
            url: {$link->getModuleLink('prestashopbeam', 'AjaxSendMail.php', array())},
            type: 'GET',
                    data: 'ajax=true&clientgroups='+ 'teste5858',
                    cache: false,
                    success: function(data) {
              $("#verifCode").show();
            }
          });
        }
    );

    {/literal}

  </script>

编辑2:链接就是为什么这会出现问题。我已将 url 更改为,url: '{/literal}{$link}{literal}',但我的浏览器工具上的请求仍然给出 404 not found 尽管路径对我来说看起来不错。

请求图片

编辑3:我修好了。问题仍然与网址有关。最终方法如下:

$link = $this->context->link->getModuleLink($this->name, 'ajax', array('ajax'=>true), true);
4

0 回答 0