douaoj0994 2014-09-08 17:22
浏览 57
已采纳

HTML表和内部按钮调用Javascript函数

I have created a table as such in file1.php:

<table id="myTable" style="width:100%;">
    <tr>
      <td><?php echo $string1; ?></td>
      <td><?php echo $string2; ?></td>
      <td><?php echo $string3; ?></td>
      <td><?php echo $string4; ?></td>
      <td><?php echo $string5; ?></td>
    <tr>

  <?php
  if(file_exists($filesDir))
  {
     foreach (new DirectoryIterator($filesDir) as $file)
     {
        if((htmlentities($file) !== ".") && (htmlentities($file) !== ".."))
        {
          /* get the file name, creation date and size */
          $filename = htmlentities($file);
          $filecdate = date("d-m-Y H:i:s", filectime($filesDir.$file));
          $filesize = filesize($filesDir.$file) /(pow(1024, 2));
        ?>
          <tr>
            <!-- print filename -->
            <td><?php echo $filename; ?></td>
            <!-- print file creation date -->
            <td><?php echo $filecdate; ?></td>
            <!-- print filesize -->
            <td><?php echo number_format($filesize, 2, '.', '\'')." MB"; ?></td>

            <!-- print button #1 -->
            <td> <?php echo "<input type=\"button\" value=\"D\" onClick=\"downloadFile($filename)\"/>"; ?> </td>

            <!-- print button #2 -->
            <td> <?php echo "<input type=\"button\" value=\"X\" onClick=\"eraseFile($filename)\"/>"; ?> </td>
          </tr>
        <?php
        }
     }
  }
  ?>
  </table>

And I have written a Javascript function in another js file:

function downloadFile(filename)
{
  $("#activity").html("<img src=\"imagenes/indicator.gif\" style=\"left: 590px;top: 74px;margin: auto;position: absolute;width: 32px;height: 32px;\" />");

    window.location.href = "file3.php?filename=" + filename;
}

Finally, in a PHP file called file3.php I use the filename value obtained with the downloadFile function, as so:

if(isset($_GET["filename"]))
{
  $filename = $_GET["filename"];

  if(file_exists($filesFolder.$filename))
  {
    set_time_limit(0);
    ob_start();
    //header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    //header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename='.basename($filename));
    //header('Content-Transfer-Encoding: binary');
    header('Expires: 5');
    header('Cache-Control: must-revalidate');
    //header('Pragma: public');
    header('Content-Length: ' . filesize($filesFolder.$filename));
    readfile($filesFolder.$filename);
    ob_end_flush();
    exit;
  }

But when I click the button from file1.php the Javascript function is not executed. Could you please help to understand where I am wrong?

  • 写回答

2条回答 默认 最新

  • dskpywvki951583595 2014-09-08 17:44
    关注

    Your $filename var will be a string. So try to add quotes.

    onClick=\"downloadFile('$filename')\"
    

    I'm not sure this is the only error, but I'm sure this is one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配