简体   繁体   中英

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

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()"

but i cannot seem to get it to work....

i am hoping to have lines written to the txt file like this

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

i would presume the line breaks are implemented with \\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....

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....

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 :(

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. However you can use javascript (read Ajax) to run a PHP file that writes whatever data you send it to a file.

For Ajax requests using jQuery is by far the easiest solution, as writing XMLHttpRequests in vanilla JS is a bit more complicated. In jQuery you would do something like:

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:

$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. You could also generate the timestamp in PHP and append that at the same time, no need to do that in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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