0

我有一个问题。如果有人提供帮助,我感到非常荣幸。

我有一个用 php 创建的网页(称为 mypage.php)。我想在这个网页上有 2 个完全相同的下载按钮;一个在页面加载或完全刷新后可用,另一个在单击另一个按钮后以 ajax 模式加载。现在,第一个下载按钮效果很好(这意味着它会在单击后下载文件),但是 ajax 按钮不起作用。事实上,我可以连接客户端和服务器端,加载我的 button.php 文件,从服务器获取响应并在我的网页中看到第二个(ajax)下载按钮。但是,不幸的是,对于 ajax 下载按钮,似乎只有 html 和 css 可以工作。我的意思是,如果您单击 ajax 下载按钮,它不会下载文件。

我的代码如下:

我的第一个下载按钮的 html 代码在我的主页 (mypage.php) 中写成 html:

<button class="gray doPrint" id="admin-sub-download-button" data-dp="download" data-func="download">Download </button>

加载包含第二个 (ajax) 下载按钮代码的 button.php 文件的 javascript 代码:

    <script>
      function replaceText() {
        var target = document.getElementById("mydivbutton");
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'http://localhost/woodmart/wp-content/plugins/myplugin/core/includes/button.php', true);
        xhr.onreadystatechange = function () {
          console.log('readyState: ' + xhr.readyState);
          if(xhr.readyState == 1) {
            target.innerHTML = 'Loading11...';
          }
          if(xhr.readyState == 2) {
            target.innerHTML = 'Loading22...';
          }
          if(xhr.readyState == 3) {
            target.innerHTML = 'Loading33...';
          }
          if(xhr.readyState == 4 && xhr.status == 200) {
            target.innerHTML = xhr.responseText;
          }
        }
        xhr.send();
      }

      var button1 = document.getElementById ("mydownloadbutton");
      button1.addEventListener("click", replaceText);
    </script>

button.php 代码:

<?php
echo ('<button class="gray doPrint" id="admin-sub-download-button" data-dp="download" data-func="download">Download </button>');    
?>

第二个下载按钮(ajax 下载按钮)添加到 html div 如下:

<div id="mydivbutton">result</div>

如果有人提供帮助,我们将不胜感激。提前致谢

4

0 回答 0