1

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

1
  • 2
    You'll need to specify what server side language you want this to work with. Commented Jul 12, 2010 at 14:11

1 Answer 1

1

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(); 
Sign up to request clarification or add additional context in comments.

3 Comments

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
ok then use method POST and not GET so you have no limit and nothing is visible into the url.
Added all the code and put it into the original question. Nothing seems to happen when I press save though?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.