views:

43

answers:

1

Hey,

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

A: 

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(); 
xdevel2000
Will that mean d = 1,4,5,6? Because this array will have hundreds of values inside it so this wouldn't be effective to put into the URL address bar? Thanks
IceDragon
ok then use method POST and not GET so you have no limit and nothing is visible into the url.
xdevel2000
Added all the code and put it into the original question. Nothing seems to happen when I press save though?
IceDragon
Got it, forgot to add action file, thanks!
IceDragon