How to save an array to a text file on the server side

After defining my javascript array as var seatsArray = []; with some contents, I'm looking to save the contents of that array to a .txt file on the server upon the user clicking a button. The text file does not exist yet, so it needs to be created.

Additionally, I am wondering if there is a way for the user to input the name of the text file they want to create by typing it into a text area. Any suggestions on how this can be achieved?

Do you have any ideas or solutions for this scenario?

Thank you, John

EDIT:

I've added the recommended code but nothing seems to happen 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>

This code block should be placed within the body section of your HTML document.

Thanks

Answer №1

One way to design a web form is by including a text field for the file name and then creating a Javascript submit event for the form. In the event handler, you can construct the URL with your data before sending it.

To convert an array into a string with comma separators, you can use the join() method like this:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Function in React not being successfully passed down between functional components

I have been accustomed to using class components and now I am transitioning into functional components in order to become more proficient with hooks. However, I have encountered an issue where I am struggling to pass a function from one functional compone ...

Capture user input from an HTML form and save it as key-value pairs in a JSON object, which can accommodate multiple input

I'm trying to figure out how to collect input from an HTML form and save it into a JSON object for AJAX use. The traditional method of $('#id').val(); won't work in this case because there are multiple fields, as shown below. Here' ...

Utilizing Ajax in conjunction with Ruby on Rails

I have a question that may be quite basic (I am new to Rails 3). I am looking to implement Ajax functionality where once a user clicks on a link, it triggers a $.post call and initiates some server-side changes. Within the _share partial file, I currently ...

javascript conceal other sections upon hovering

There are 4 list items (<li>) that I want to use as triggers for linked images. In this project, I am using vanilla JavaScript as jQuery is not allowed. Below is the code snippet: var children = document.querySelectorAll('#resistorContent > ...

Save both old and current data that has been received into a C# array

I am attempting to save both previous and current data from a text box into an array. The content of the textbox undergoes constant updates until the program is ended. There is only one line, but that single line always gets updated. I aim to retain the p ...

Attempting to unveil concealed download URLs

Trying to extract download links from a website, but the format is as follows: <form action="" method="post" name="addondownload" id="addondownload" > <input type="hidden" name="addonid" id="addonid" value="2109" /> <input class="re ...

Creating a personalized filter list in Vue Instant Search: A step-by-step guide

Currently, I'm utilizing Laravel Scout with Algolia as the driver. Vue is being used on the front end and I've experimented with the Vue instant search package, which has proven to be very effective. The challenge I am encountering involves cust ...

Issue encountered when running a minification build on Angular 5

After successfully updating my Single Page Application (SPA) from Angular 4 to Angular 5 along with all dependencies, everything seemed to be working well. Both the development and production builds were functioning without any errors or warnings. However ...

Rotate the pixel information within a 2D array by a specified number of degrees using either PseudoCode or Python3

As part of a school project, our informatics teacher has challenged us to innovate in image manipulation by working with Colour-Objects represented in an array. Each Colour-Object consists of 4 integer values (0-255 for Red, Green, Blue, and Alpha). The ta ...

What could be the reason that the results of my quick sort function are not appearing on the screen

I'm having trouble getting any output from this code. Can someone help me figure out what's wrong? function Ascending() { var array = new Array(); array[0]=parseInt(document.getElementById("1").value); array[1]=parseInt(document.getElementById(" ...

Creating duplicates to switch between HTML elements using hammer.js

I am attempting to switch between two div elements, meaning replacing one with the other while preserving events and everything. Both divs have Hammer events set up to detect swipes. However, when I perform a swap in my project, the elements lose their Ha ...

Prometheus nodejs app already contains a metric by that name

Encountering a problem where the metric has already been registered when attempting to publish metrics from a service. To work around this issue, the register.removeSingleMetric("newMetric"); method was used. However, this method clears the register and pa ...

What sets $(document).on apart from ($ document).on in CoffeeScript?

One of my buddies is incorporating ($ document).on into his CoffeeScript script. I'm curious to know if this differs from the typical $(document).on and, if it does, how so? ...

Tips for confirming the validity of an email address

Similar Question: How to validate an email address in PHP Could someone please assist me? I am facing issues with validating the email address for the code below. <?php if ($_POST) { $expected = array('name', 'email', &a ...

Best practices for efficiently storing and retrieving data from a large list in PHP

Hello and thank you in advance for your assistance, I am currently faced with a challenge involving a list of one million names and their corresponding nicknames. Each name is paired with only one nickname, even if they happen to be the same. My usual ap ...

Tips for utilizing AJAX in Angular to load web pages in partials

My goal is to display a loader for each container while making an AJAX call to retrieve content. Initially, I want both div columns to show the loader, and then once the AJAX call is successful, I need to hide the loader for that specific column. Despite ...

Endless spinning using snap.svg

How do I make an SVG element rotate endlessly? Is there a way to make it rotate continuously without stopping? While Snap provides a way to rotate an SVG element by a specific amount, I want to know how to create an infinite rotation effect. Is there a so ...

My journey doesn't begin at zero

This piece of code triggers an alert message saying 'test 1', followed by an alert indicating the number 8! uri = 'http://www.scriptcopy.com/'; compareuris = new Array(); compareuris[0] = 'http://www.scriptcopy.com/'; compare ...

Tips on creating a unique d3js tree design

I am a beginner when it comes to d3js and javascript in general. My goal is to create an interactive IP administration overview using d3js by modeling json data. I know that the key tool for this job is likely d3.layout.tree, which will provide me with the ...

Create a new visual masterpiece using Canvas by repurposing an

I am currently working on the following code snippet; export default async function draw(elRef : RefObject<HTMLCanvasElement>, tileData : TileProps) { const canvas = elRef.current!; const ctx = canvas.getContext('2d')!; ctx.clearRect( ...