0

I am very (very) noob at Web Development and I need help.

To summarize, I collect some data from some HTML forms and make a string out of it in my JavaScript code (global variable _customScript).

<SELECT id="product-green" NAME="Liste" class="select_green" onChange="mviewer.customControls.s2product.updateBandes()">
    <OPTION label="B01" value="B01"> B01 </OPTION>
    <OPTION label="B02" value="B02"> B02 </OPTION>
    <OPTION label="B03" value="B03"> B03 </OPTION>
    <OPTION label="B04" value="B04"> B04 </OPTION>
    <OPTION label="B05" value="B05"> B05 </OPTION>
    <OPTION label="B06" value="B06"> B06 </OPTION>
    <OPTION label="B07" value="B07"> B07 </OPTION>
    <OPTION label="B08" value="B08"> B08 </OPTION>
    <OPTION label="B09" value="B09"> B09 </OPTION>
   </SELECT>

updateBandes: function() {

var redBand = $('.select_red').options[$('.select_red').selectedIndex].value;
var greenBand = $('.select_green').options[$('.select_green').selectedIndex].value;
var blueBand = $('.select_blue').options[$('.select_blue').selectedIndex].value;

_customScript = 'return [2.5*'+ redBand + ', 2.5*' + greenBand + ', 2.5*' + blueBand + '];'

_updateEvalScriptFile();
        },

Now I need to create a function (_updateEvalScriptFile) in order to put this String inside a file and save it. This file already exists on the server and I have its URL, I just need to clear everthing that is in this file and replace it with this String.

I am pretty sure this is a simple task but I can't find any clear explicit explanation. Can you help me please ? Ps: Sorry if I said something wrong or illogical, as I said I'm pretty noob at web development

  • Hit your server with ajax ?? ,do file operations on server.Obviously you cant access server file from client side js – Shubham Dixit Jun 08 '19 at 16:00
  • You cannot do that directly with client side js. You need to send the data to some server side script and have that server script do the saving – Patrick Evans Jun 08 '19 at 16:01

2 Answers2

0

As the comments indicate, you cannot do that directly, however if you create a script in the server that will handle that call it could be possible by retrieveing the script from the server and then execute it.

You are already using JQuery so you could make an ajax POST request and a GET request to get back your data. For more information on the subject there is a very similar question here: Edit serverside files in JS and I cant type the code because I'm a noob and I'm on my phone but if you look at JQuery documentation youl find the code: https://api.jquery.com/jQuery.getScript/

Also refer to: Can javascript access a filesystem?

Furthermore another solution depending on what technology you use, would be HTML5 local storage: https://www.w3schools.com/html/html5_webstorage.asp

Erick Ramirez
  • 157
  • 1
  • 3
  • 9
0

If the file exists on a server and you have URL address directly leading to the file (the file must be in a public space of the server so that it can be directly accessed by using its URL), it is a simple task.

See for example here: HTML - read .txt file from URL location in javascript

On the other hand, if you would be trying to access a file locally on a client file system, this is not possible due to security reason. You would have to use a file upload dialog for that.

Mikulas
  • 407
  • 4
  • 5