简体   繁体   English

javascript将.txt文件写入onclick

[英]javascript write to .txt file onclick

i have some swf apps that open in a lightbox when a link to an swf file is clicked on the page 单击页面上的SWF文件链接时,我在灯箱中打开了一些SWF应用程序

it is purely standard link required to trigger the lightbox 这是触发灯箱所需的纯标准链接

<a href"http:// {url} /swf/ {file} .swf">Name</a>

i have been trying to implement a script to write a timestamp (date('U') in php coding, as that is the format i need the timestamp) and the "href" value of the link clicked to a txt file ... i have been using onClick="function()" 我一直试图实现一个脚本来编写时间戳(用php编码编写date('U'),因为这是我需要的时间戳格式),并且单击链接到txt文件的链接的“ href”值...我一直在使用onClick =“ function()”

but i cannot seem to get it to work.... 但我似乎无法使其正常工作。

i am hoping to have lines written to the txt file like this 我希望将这样的行写入txt文件

1328984654|http:// {url} /swf/ {file} .swf
1328984829|http:// {url} /swf/ {file2} .swf

i would presume the line breaks are implemented with \\r\\n 我认为换行符是用\\ r \\ n实现的

i dont necessarily need the file extention to be written, however i would presume it would be easier just to write the full link url.... 我不必一定要写文件扩展名,但是我认为只写完整的链接URL会更容易。

the method i have been using was hidden form that submits onclick but its not very economical, as i need hidden iframe blah blah ...... too much work, when i think this could be implemented in one script.... 我一直在使用的方法是提交onclick的隐藏形式,但是它不是很经济,因为我需要隐藏的iframe等等……当我认为可以在一个脚本中实现时,工作量太大。

i can easily change my existing link generating script, so that it can add 我可以轻松更改现有的链接生成脚本,以便可以添加

onClick="new_function()"

or any variation of this to be able to link it to the function.... 或其任何变化形式,以便能够将其链接到该功能。...

i am not very experienced with javascript, and i know the click script will need to be client side :( 我对javascript不太有经验,我知道点击脚本需要在客户端:(

thanks in advance if anyone can help me, cheers 预先感谢如果有人可以帮助我,加油

Javascript does not have access to the server file system and can not write anything to a file. Javascript无法访问服务器文件系统,也无法将任何内容写入文件。 However you can use javascript (read Ajax) to run a PHP file that writes whatever data you send it to a file. 但是,您可以使用javascript(读取Ajax)来运行PHP文件,该文件会将发送给它的所有数据写入文件。

For Ajax requests using jQuery is by far the easiest solution, as writing XMLHttpRequests in vanilla JS is a bit more complicated. 到目前为止,对于使用jQuery的Ajax请求而言,这是最简单的解决方案,因为在普通JS中编写XMLHttpRequests会更加复杂。 In jQuery you would do something like: 在jQuery中,您可以执行以下操作:

HTML: HTML:

<a href"http:// {url} /swf/ {file} .swf" id="mylink">Name</a>

- --

$("#mylink").click(function() {
    $.ajax({ 
       type: 'POST', 
       data: {swf : '1328984654|http: {url} /swf/ {file} .swf'}, 
       url: 'write_to_file.php',
       success: function(data) {
          alert('file saved');
       },
       error: function() {
          alert('error');
       }
    });
});

In PHP you'll get the data from the $_POST superglobal like so: 在PHP中,您将像这样从$ _POST超全局变量获取数据:

$data = $_POST['swf'];

Then use file_put_contents or one of the many other solutions for saving the data to your file, and since you've had this working with a form I'm not going to go into that part. 然后使用file_put_contents或许多其他解决方案之一将数据保存到文件中,并且由于您已经使用了表格,因此在此不做介绍。 You could also generate the timestamp in PHP and append that at the same time, no need to do that in JS. 您还可以在PHP中生成时间戳并将其附加在同一时间,而无需在JS中进行。 It is of course a bit more involved than this, for instance you should do the normal isset check on $_POST, and sanitize any data that comes from the clientside, but if you do a little reading and testing on your own you'll figure it out in no time, it's really not that hard, and just a few lines of code is enough to get a basic example of what you are trying to do up and running. 当然,它比这要复杂得多,例如,您应该对$ _POST进行常规的isset检查,并清理来自客户端的所有数据,但是如果您自己进行一些阅读和测试,就会发现它很快就可以解决,实际上并不难,只需几行代码就足以获取您要尝试构建和运行的基本示例。

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

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