Javascript 从另一个php文件执行php文件

Javascript 从另一个php文件执行php文件,javascript,php,Javascript,Php,在执行操作时,是否可以从另一个php文件触发php文件的执行?更具体地说,我有一个用echo生成的锚,它有一个pdf文件的href。除了下载pdf,我还想在表格中插入一些信息。这是我的代码,不起作用: require('./database_connection.php'); $query = "select author,title,link,book_id from book where category='".$_REQUEST['categorie']."'"; $res

在执行操作时,是否可以从另一个php文件触发php文件的执行?更具体地说,我有一个用echo生成的锚,它有一个pdf文件的href。除了下载pdf,我还想在表格中插入一些信息。这是我的代码,不起作用:

require('./database_connection.php');
    $query = "select author,title,link,book_id from book where category='".$_REQUEST['categorie']."'";
    $result = mysql_query($query);
    $result2 = mysql_query("select user_id from user where username='".$_REQUEST["username"]."'");
    $row2 = mysql_fetch_row($result2);
    while($row= mysql_fetch_row($result))
   {
       echo '<h4>'.$row[0].' - '.$row[1].'</h4>';
       if(isset($_SESSION["username"]) && !empty($_SESSION["username"]))
       {
           echo '<input type="hidden" name="id_carte" value="'.$row[3].'">';
           echo '<input type="hidden" name="id_user" value="'.$row2[0].'">';
           echo '  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
                   <script language="javascript">
                   function insert_download() {
                    $.ajax({
                    type: "GET",
                    url: "insert_download.php" ,
                    success : function() { 
                    location.reload();
                    }
                    });
                    }
                  </script>
                  <a onclick="insert_download()" href="'.$row[2].'" download> download </a>';
                   }
require('./database_connection.php');
$query=“从类别为“”的图书中选择作者、标题、链接、图书id”。$\u请求['category']。”;
$result=mysql\u query($query);
$result2=mysql\u查询(“从username='”的用户中选择用户id”。$\u请求[“username”]。“'””;
$row2=mysql\u fetch\u行($result2);
while($row=mysql\u fetch\u row($result))
{
回显'.$row[0].-'.$row[1].';
if(设置($会话[“用户名”])和&!空($会话[“用户名”]))
{
回声';
回声';
回声'
函数插入_下载(){
$.ajax({
键入:“获取”,
url:“insert_download.php”,
成功:函数(){
location.reload();
}
});
}
';
}
下面是insert_download.php:

    <?php
require('./database_connection.php');
$query = "insert into download(user_id,book_id,date)values(".
          $_REQUEST["id_carte"].",".
          $_REQUEST["id_user"].",".
          date("Y-m-d h:i:s").")";
mysql_query($query,$con); 
mysql_close($con);
?>


有人能帮我吗?谢谢!

为什么不使用重定向而不是“复杂”的AJAX


回声';
回声';
回声';
下载_pdf.php

<?php
require('./database_connection.php');
...
mysql_close($con);

header("location: " . $_GET['redirect']);

您缺乏调试的基本技能。如果我是您,我应该:

  • 使用支持“网络”检查选项卡的浏览器,例如:Chrome


  • 试着点击链接,正如我正确理解的,你想显示一个链接,当用户点击该链接时

    • 一些数据被插入到数据库或其他地方
    • 用户看到一个下载对话框,允许他下载文件
    如果这是正确的,您可以使用以下代码:

    在您的网页上:

    <a href="result.php?file=dummy.pdf">download</a>
    
    
    
    result.php

    <?php
    
    $file = isset($_GET['file']) ? $_GET['file'] : "";
    
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <title>Downloading...</title>
            <script type="text/javascript">
    
            function redirect(url) {
                //window.location.replace(url);
                window.location.href = url;
            }
    
            </script>
        </head>
        <body>
            Download is starting...
            <script type="text/javascript">
    
            redirect("http://example.com/download.php?file=dummy.pdf");
    
            </script>
        </body>
    </html>
    
    <?php
    
    $file = isset($_GET['file']) ? $_GET['file'] : "nullfile";
    $file_url = "download_dir_or_something/".$file;
    
    // Put some line in a log file...
    file_put_contents("logfile.txt", "successful on ".date("Y-m-d H:i:s")."\n", FILE_APPEND);
    // ...or anything else to execute, for example, inserting data into a database.
    
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=\"".basename($file_url)."\"");
    readfile($file_url);
    
    ?>
    
    
    正在下载
    函数重定向(url){
    //window.location.replace(url);
    window.location.href=url;
    }
    下载正在开始。。。
    重定向(“http://example.com/download.php?file=dummy.pdf");
    
    下载.php

    <?php
    
    $file = isset($_GET['file']) ? $_GET['file'] : "";
    
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <title>Downloading...</title>
            <script type="text/javascript">
    
            function redirect(url) {
                //window.location.replace(url);
                window.location.href = url;
            }
    
            </script>
        </head>
        <body>
            Download is starting...
            <script type="text/javascript">
    
            redirect("http://example.com/download.php?file=dummy.pdf");
    
            </script>
        </body>
    </html>
    
    <?php
    
    $file = isset($_GET['file']) ? $_GET['file'] : "nullfile";
    $file_url = "download_dir_or_something/".$file;
    
    // Put some line in a log file...
    file_put_contents("logfile.txt", "successful on ".date("Y-m-d H:i:s")."\n", FILE_APPEND);
    // ...or anything else to execute, for example, inserting data into a database.
    
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=\"".basename($file_url)."\"");
    readfile($file_url);
    
    ?>
    
    
    
    我没有收到任何错误,只是执行insert_download.php不起作用。我已经使用alert来验证函数是否已执行且正常,所以我猜问题出在ajax部分。请使用控制台检查它是否调用了您的文件。只需在文件中回显您的查询并使用控制台输出进行检查。我通过响应重新编写,也许它可以警告:你的代码是。我不是说你想要什么。你想在某个页面上显示一个链接,然后如果用户单击该链接,1.执行一些代码,2.出现一个下载对话框,允许用户下载PDF文件。对吗?我也试过了,它不起作用。此外,我需要下载该文件每次下载pdf时执行,即每次用户单击锚定时