简体   繁体   English

一次提交表单并下载文件

[英]Submitting a form and downloading a file in one action

I have a file download page where I want people who download my file to fill in a form.我有一个文件下载页面,我希望下载我的文件的人填写表格。 The form looks like this:表格如下所示:

        <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="download">
            <tr>
                <td width='30%' class="left">Your name</td>
                <td width='70%'><input name="name" type="text" value=""></td>
            </tr>
            <tr>
                <td class="left">Your organisation</td>
                <td><input name="organisation" type="text" value=""></td>
            </tr>
            <tr>
                <td class="left">The place your map will cover</td>
                <td><input name="area" type="text" value=""></td>
            </tr>
            <tr>
                <td class="left">Your email</td>
                <td><input name="email" type="text" value=""></td>
            </tr>
            <tr>
                <td class="left">&nbsp;</td>
                <td><input name="MM_load" type="hidden" value="download">
                    <input style="margin-top: .8em" type="submit" class="button" value="Download Now"></td>
            </tr>
        </form>

In the PHP before the HTML I have:在 HTML 之前的 PHP 中,我有:

if ((isset($_POST['MM_load']))&&($_POST['MM_load']=='download')) {
    $organisation = mysqli_real_escape_string($db, stripslashes($_POST['organisation']));
    $area = mysqli_real_escape_string($db, stripslashes($_POST['area']));
    $name = mysqli_real_escape_string($db, stripslashes($_POST['name']));
    $email = mysqli_real_escape_string($db, stripslashes($_POST['email']));
    $query_insert = "INSERT INTO downloads (organisation, area, name, email) VALUES ('$organisation', '$area', '$name', '$email')";
    $result = $db->query($query_insert) or die($db->error.__LINE__);


echo "<script>window.getElementsByClassName('dl_link').click();</script>";

  $insertGoTo = "thanks.php";
  echo "<script type='text/javascript'>window.location.href='".$insertGoTo."'</script>";
}

Then buried in the body of the page is the download link:然后埋在页面正文中的是下载链接:

<a style="visibility:hidden" class="dl_link" href="https://example.com/defib-package/defib.1.1.zip" download></a>

The intention is:意图是:

  1. The user fills in the form and clicks the Download Now button.用户填写表单并单击立即下载按钮。

  2. The page reloads and inserts the data in the database.该页面重新加载并将数据插入到数据库中。

  3. The javascript does a virtual click on the download link so the file is downloaded to the user's Downloads folder. javascript 对下载链接进行虚拟点击,以便将文件下载到用户的下载文件夹中。

  4. The user is then re-directed to the thanks.php page.然后用户被重定向到thanks.php 页面。

Steps 1, 2 and 4 work, but 3 doesn't.步骤 1、2 和 4 有效,但步骤 3 无效。

I've tried modifying the download link so it can be clicked manually.我已经尝试修改下载链接,以便可以手动单击它。 It then works.然后它起作用了。 I've tried putting the PHP (including the embedded javascript) in the head of the page that doesn't help.我试过将 PHP(包括嵌入的 javascript)放在没有帮助的页面的头部。 I'm a complete novice on javascript so I expect the problem lies there, but it's code I have copied from another answer on Stack Overflow.我是 javascript 的完全新手,所以我希望问题出在那里,但它是我从 Stack Overflow 上的另一个答案复制的代码。 Where should I go from here?我应该从这里去哪里?

EDIT in response to coredo's reply.编辑以响应 coredo 的回复。 Coredo helped me make some progress but I'm not there yet. Coredo 帮助我取得了一些进展,但我还没有做到。 The timeout works nicely but I'm getting "Uncaught TypeError: window.getElementById is not a function" in the console for the link click.超时工作得很好,但我在链接单击的控制台中收到“未捕获的类型错误:window.getElementById 不是函数”。 I've swapped from class to id, and put it in a function but all the attempts throw up something along the same line.我已经从 class 切换到 id,并将它放在一个函数中,但是所有的尝试都在同一条线上抛出了一些东西。 My latest code is我的最新代码是

$insertGoTo = "thanks.php";
  echo "<script type='text/javascript'>
    alert('Just starting');
    function dl() {window.getElementById('dl_link').click();}
    dl();
  alert('Part way');
  setTimeout(function(){ window.location.href='".$insertGoTo."' }, 5000);
  </script>";

EDIT 2 Cracked it!编辑 2 破解它!

  1. getElementById doesn't work on a window element. getElementById 不适用于窗口元素。 It had to be changed to document.必须将其更改为文档。

  2. It appears that the click was trying to execute before the DOM was created.似乎在创建 DOM 之前点击试图执行。 So I wrapped it in window.onload.所以我把它包裹在window.onload中。 The final form of the script is脚本的最终形式是

    $insertGoTo = "thanks.php"; $insertGoTo = "thanks.php"; echo " window.onload = function(){ document.getElementById('dl_link').click(); } setTimeout(function(){ window.location.href='".$insertGoTo."' }, 2000); "; echo " window.onload = function(){ document.getElementById('dl_link').click(); } setTimeout(function(){ window.location.href='".$insertGoTo."' }, 2000); " ; } }

Change改变

echo "<script>window.getElementsByClassName('dl_link').click();</script>";

to:到:

echo "<script>document.getElementsByClassName('dl_link')[0].click();</script>";

as getElementsByClassName may return multiple objects.因为 getElementsByClassName 可能返回多个对象。

Also it is possible that window.location.href redirect will redirect user too fast and download might not start.此外,window.location.href 重定向可能会过快地重定向用户并且可能无法开始下载。 Try to add some delay here using setTimeout:尝试使用 setTimeout 在此处添加一些延迟:

setTimeout(function(){ window.location.href='".$insertGoTo."' }, 2000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM