javascript - 如何编写一个 (php) Web 应用程序以向客户端发送一个 .txt 文件,其中包含客户端已插入 JS 提示框的值

标签 javascript php

我希望开发一个(玩具)应用程序,1) 让用户在 JS 提示框中填写几个值,将它们写入 .txt 文件中,2) 将后者发送回用户.

我可以做其中任何一个,但不能将它们组合在一个应用程序中。

我有这两个简短的脚本:

main.html

<html>
<head>
<script type="text/javascript">
        function myFunction() {
 var person = prompt("Please enter your name", "Harry Potter");
 var mail = prompt("Please enter your email", "Harry Potter");
 document.getElementById("person").value = person;
 document.getElementById("mail").value = mail;
 document.getElementById("form").submit();   <<<< NOTE
 }

注意,提交似乎会导致 readfile() 失败;下面

 </script>
 </head>
 <body>

 <form id="form" action="minor.php?file=abc.txt" method="post">
 <input type="hidden" name="person" id="person" />
 <input type="hidden" name="mail" id="mail" />
 </form>

 <a onclick=myFunction(); href="minor.php?file=abc.txt">see abc.txt</a>';
 </body> 
 </html>

注意我尝试了上面的 action="minor.php?file=abc.txt"href="minor.php?file=abc.txt" (不是同时两者!),并且无论哪种方式都缺少一些东西(请参阅下面我得到的不同输出)

次要.php

<?php  

 $file_directory = "Files";  
 $file = $_GET['file'];  
 $file_array = explode('/', $file);  
 file_array_count = count($file_array);  
 $filename = $file_array[$file_array_count-1];  
 $file_path = dirname(__FILE__).'/'.$file_directory.'/'.$file; 

 if (is_writable($file_path)) {
 if (!$handle = fopen($file_path, 'a')) {
     echo "Cannot open file ($file_path)";
     exit;
       }

 foreach($_POST as $key => $req){  // or use $_REQUEST
    if (fwrite($handle, $key."--".$req."\n")== FALSE){  
    echo "Cannot write to file ($file_path)";
    exit;
   }
   }
 fclose($handle);

 } else {
  echo "The file $file_path is not writable";
  }

 if(file_exists($file_path)) {
header('Content-type: text/plain'); 
header("Content-disposition: attachment; filename={$filename}");
readfile($file_path); //Read and stream the file
}
else {
echo "Sorry, the file does not exist!";
}                              
?> 

这是当我打乱几个数据时得到的不同输出:

case A
document.getElementById("form").submit(); included
action="" (empty)
href="minor.php?file=abc.txt"
----
DO download abc.txt
Nothing written into abc.txt
access.log: "GET .../minor.php?file=abc.txt HTTP/1.1" 200 13

case B
//document.getElementById("form").submit();  // Commented out
action="" (empty)
href="minor.php?file=abc.txt"
-----
DO download abc.txt
Nothing written into abc.txt (clear why)
access.log:  "GET .../minor.php?file=abc.txt HTTP/1.1" 200

case C
document.getElementById("form").submit();  included
action="minor.php?file=abc.txt"
href="" (empty)
-----
DOES NOT download abc.txt
access.log "GET .../main.html HTTP/1.1" 304 (error??)

欢迎您的帮助。

最佳答案

以下代码满足我的要求:

(minor.php 如上所述,但使用 foreach($_GET ... ) 而不是 foreach($_POST ... ) )

ma​​in.html

<html>
<head>
<script type="text/javascript">
function myFunction() {
var person = prompt("Please enter your name", "Harry Potter");
var mail = prompt("Please enter your email", "Harry Potter");
    document.getElementById("clickhere").innerHTML="";
    document.getElementById("mainlink").innerHTML="please download now"; 
    document.getElementById("mainlink").href="minor.php?file=abc.txt&"+"person="+person+"&mail="+mail+""; 
    document.getElementById('mainlink').click();
}
</script>
</head>
<body>
<div onclick=myFunction(); id="clickhere">click here</div>
<a  href="#" id="mainlink"></a>
</body>
</html>

我欢迎其他带有改进/替代代码的答案。

关于javascript - 如何编写一个 (php) Web 应用程序以向客户端发送一个 .txt 文件,其中包含客户端已插入 JS 提示框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42349872/

相关文章:

javascript - Chrome 和 Firefox 之间的 WebRTC 重新协商

javascript - 如何使用 javascript 将值插入到与另一个字符串匹配的位置的字符串中?

php - 我有在 SDK 上运行但不在设备上运行的 Android 数据库应用程序

javascript - 我如何判断用户是否使用 Brave 作为他们的浏览器?

xml - 遍历 XML 树获取属性值

php - 何时检查查询是否成功完成

php ssh linux 屏幕执行

javascript - 仅在禁用 Flash 时显示颜色框关闭按钮

javascript - 滚动时修复侧边栏,直到到达页脚(使用 Bootstrap 类)

javascript - Alexa API 调用没有返回任何内容