-1

Tried all solutions.. none of them seem to work for me.

Basically, I have this website UPLOADED on the server that has a button which when clicked does a simple task of loading a file from the server, displaying it and adding +1 to it and then saving it back.

The main question now arises, I tried all the ways which I could find on the net to save the file back but it doesn't seem to work for me. Any specific advice on how can I fix this issue using JS?

UPDATE - One way I am using is - (Doesn't work tho)

fso = new ActiveXObject("Scripting.FileSystemObject");
            var s = fso.CreateTextFile("TOTAL.TXT", true);
            var text = test1;
            s.WriteLine(text);
            s.Close();

Where test1 is the name of the variable which i am trying to write.

Akshit Sharma
  • 49
  • 1
  • 7
  • May you post some of the `ways which I could find on the net to save the file back` so we know what you exactly did and how we could help you? – lippoliv Aug 29 '16 at 09:45
  • by pure JS do you mean getting rid of jQuery from the client code? – Jaromanda X Aug 29 '16 at 09:45
  • 2
    Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – eisbehr Aug 29 '16 at 09:45
  • When you have tried **all** solutions, then there is no solution left. ;) – eisbehr Aug 29 '16 at 09:45
  • 1
    @eisbehr - should put that on a t-shirt :p – Jaromanda X Aug 29 '16 at 09:46
  • This can not be done in JS, as JS can not write to files, neither on the client nor on the server side (unless we're talking about node.js). – Mike Scotty Aug 29 '16 at 09:46
  • Javascript is server sided (or I would expect a nodeJS tag) and client sided javascript on it's own can't write to a server. You can make a HTTP request, using ajax for example and ash a PHP file to do what you want to do. – online Thomas Aug 29 '16 at 09:46
  • 1
    it can't be done using pure javascript. You must have a server-side script to handle your request – Tony Vincent Aug 29 '16 at 09:46
  • it can be done in pure JS both client and server, if the server happens to be nodejs (or something like it) ... I think the question may be how to remove jquery from the client side code, server side is untouched (that's my opinion - would be nice if the OP came back to clarify some of these questions from well intentioned folk) – Jaromanda X Aug 29 '16 at 09:47
  • @TonyVincent Like what? – Akshit Sharma Aug 29 '16 at 09:47
  • Like a php script – Tony Vincent Aug 29 '16 at 09:48
  • @TonyVincent or javascript (nodejs) – Jaromanda X Aug 29 '16 at 09:48
  • Now you would confuse someone here. Now js is server-side too? Whoa! :D @JaromandaX – eisbehr Aug 29 '16 at 09:49
  • @eisbehr - nodejs can be used to write fairly decent server side code (I'm not really sure how to take your comment to be honest) – Jaromanda X Aug 29 '16 at 09:50
  • yes nodejs is awesome – Tony Vincent Aug 29 '16 at 09:53
  • @JaromandaX - It was a bit of irony, sorry. I think it will confuse him even more, that js can be server-side too. ;) – eisbehr Aug 29 '16 at 09:53
  • Edited question further. Please have a check once again. Thanks... – Akshit Sharma Aug 29 '16 at 09:53
  • @eisbehr - all good - the problem is, the question is extremely unclear – Jaromanda X Aug 29 '16 at 09:53
  • @AkshitSharma - did you say you have jQuery that works as you want? because jQuery is just sugar for javascript - if it can be done with jQuery, it already **is** being done with javascript. There's no need for activex (IE7 is dead anyway) - jquery doesn't need it, therefore you don't. Please clarify what **the issue actually is** – Jaromanda X Aug 29 '16 at 09:56
  • Is this possible via AJAX? – Akshit Sharma Aug 29 '16 at 10:17

2 Answers2

1

assuming that you have php on server try this js

var data = new FormData();
data.append("data" , 1);
var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
xhr.open( 'post', '/path/to/php', true );
xhr.send(data);

php script

<?php
if(!empty($_POST['data'])){
$data = $_POST['data'];
$fname = "filname.txt"

$file = fopen("upload_or_whatever_path/" .$fname, 'w') or die("Unable to open file!");// open file
fwrite($file, $data);
fclose($file);
}
?>
Tony Vincent
  • 13,354
  • 7
  • 49
  • 68
0

The code you've shared belongs to the Windows Scripting Host platform. That's an automation solution for Microsoft Windows that's by no means intended for public web sites.

File upload implementation requires a proper server-side programming language (PHP, JSP, Node...). Client side code can be as simple as a static HTML form.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360