weixin_33686714 2012-08-24 13:05 采纳率: 0%
浏览 924

将javascript数组写入文件

i have loaded a file into an array using ajax and after splitting it i need to save it to the file again.This all happens onClick of a button.

function updatetags(){

 var alreadyexistingtags;
 var responsetext;

 var r2 = new XMLHttpRequest();
 r2.open('GET', 'tagsupdated.txt', true);
 r2.send(null);

 r2.onreadystatechange = function() {
    if (r2.readyState == 4 && r2.status==200) {

        responsetext=r2.responseText;

        alreadyexistingtags=responsetext.split(' ');
        }       
  }
 }

i understand that javascripts are not server side and that's why i cannot do what i want,but i'm sure there must be a way to write alreadyexistingtags[ ] to tagsupdated.txt.Any help?Perhaps i should somehow pass the array to PHP?And if so how is that possible given that PHP gets executed when the page loads,when i need to wait for the button to be pressed?

  • 写回答

2条回答 默认 最新

  • 7*4 2012-08-24 13:10
    关注

    You're going to have to use AJAX or some other method to send the data back to your server, which can then write the file out.

    To do this, just create a new script on your server, say writearray.php, which accepts the Javascript array as input. Then use AJAX to send a request to that file with your array.

    The PHP file would look something like so (this is a highly simplified example):

    <?php
    file_put_contents("where_you_want_the_file.txt", $_POST['array']);
    ?>
    

    It looks like you're just storing your Javascript array as a space separated list, so the Javascript would look something like this:

    var str = your_array.join('%20');   // URL encoded spaces separating array entries
    var params = "array=" + str;
    var http = new XMLHttpRequest();
    http.open("POST", "your_script.php", true);
    
    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
    
    http.onreadystatechange = function() {
        if(http.readyState == 4 && http.status == 200) {
            // Do something on success?
        }
    }
    http.send(params);
    

    The way this works, is you're sending a POST with a field named array, which holds the textual representation of your Javascript array. The PHP code checks $_POST['array'] to get this value, then writes it to your file.

    Note that if you want to do anything more complicated, you should look into using JSON. And also as always be very careful with what you do with user data.

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?