简体   繁体   中英

Writing an array to a (server-side) text file

So I've got my javascript array (var seatsArray = [];), let's say it has some contents. I want to write the contents of that array to a .txt file on the server when the user clicks a button. The text file will not already exist so it needs to be created.

Also, if anyone knows how I could allow the user to specify the name of the text file to be created by typing it in a text area, that would be great.

Any ideas?

Many thanks John

EDIT:

Added the suggested code, however, nothing happens when I hit save?

<form id="my_form" action="">
<input type="text" id="file_name" rows="1" cols="20">
<a href="javascript: SubmitForm()">Save</a>
</form>

<script type="text/javascript">
function submitform();
{
var d = seatsArray.join();
var url = "/txtfiles/"+d + "&file_name=" + 
document.getElementById("file_name").value;

document.getElementById("my_form").action = url;
document.getElementById("my_form").method = "POST";
document.getElementById("my_form").submit();
}
</script>

That is all in the body section.

Thanks

You can layout a web form with, among other things, a text field for file name. Then write a Javascript submit event for the form and, in its handler, before send the data build the url with your data.

For the array you can join its data so its converted into a string with a comma separator

var seatsArray = [1,4,5,6];
var d = seatsArray.join(); // "1,4,5,6"

var url = "http://my_site/my_file.php?my_array="+d + "&file_name=" + 
document.getElementById("file_name").value;

document.getElementById("my_form").action = url;
document.getElementById("my_form").method = "POST";
document.getElementById("my_form").submit(); 

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