-1

I have a list of file download urls that will be displayed to user and upon user clicking it will trigger download. I am using the following code:

echo "<a href=$val1 download=\"$fileId.$fileformat\".>Click Here To Download</a>";

But how do I change this so that a function is called instead of download as I need to do some processing before download. I used the following but it is not working:

echo "<a href=$val1 onclick=func($val1,$fileId,$fileformat)>Click Here To Download</a>";

Is there any other way to call a function from hyperlink?

I tried using if(isset($_GET['id'])) way but I have a list of links which I need to display to user and when user clicks one than pass to function to trigger download.

4

4 回答 4

2

您不能直接从 html 调用 php 函数。

您的问题的解决方案是调用一个 php 文件,该文件将运行该函数并返回文件以供下载

前任。

<a href="download-file.php?val1=$val1&fileId=$fileId&fileFormat=$fileFormat">Click Here To Download</a>

您的 donwload-file.php 文件检查并验证 $_GET 并且您将运行返回结果所需的函数。

于 2013-07-04T08:04:28.357 回答
0

启动 php 函数“onclick”的唯一方法是使用 $_GET[]。

您有两种方法可以做到这一点:

  1. 你href一个变量,例如。触发。

    <a href="?variable=value">click</a>
    <?php
    if(isset($_GET['variable'])) {
       $variable = $_GET['variable'];
    
       /* now you can either directly execute your
          function, create a switch or an if/elseif block */
    
       switch($variable) {
          case "file1":
             function myFunction() {
             /* your function */
             }
          break 1;
       }
    }
    ?>
    
  2. 您创建一个类(OOP)并将其包含在您的文件中:

    <?php
    $trigger = $_GET['trigger'];
    include "include_path/class_name.php";
    ?>
    

班级:

    <?php
    class ClassName {

      function ClassName() {
      global $trigger;

        if($trigger == "value1") {
        // your function
        }

        if($trigger == "value2") {
        $this->myFunction();
        }

        if($trigger == "value3") {
        $otherClassName->myOtherFunction();
        }

      }

    }
    ?>
于 2013-07-04T08:24:14.517 回答
0

尝试这个,

echo "<a href='javascript:;' onclick=func('".$val1."','".$fileId."','".$fileformat."')>
   Click Here To Download</a>";
于 2013-07-04T07:55:09.990 回答
0
$("#link_id").click(function() {
//Your code here
});


echo "<a href=$val1 download=\"$fileId.$fileformat\". id='link_id'>Click Here To Download</a>";
于 2013-07-04T08:01:25.420 回答