1

I need to store data in server-side.I tried to make an Ajax call to PHP: upload.html:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<style>
#test{
padding:20px 50px;
background:#ccc;
color:#000;
}
</style>
<script>
$(function(){
$('#test').click(function(){
    $.ajax({
        url: "http://localhost:8012/myFolder/upload.php",
  type : 'POST',
        data: {"foo": "bar"},
        processData: false,
        contentType: 'application/json'
    });

});
});
</script>
</head>
<body>

<button id="test">KLICK</button>


</body>
</html>

upload.php:

<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w');
fwrite($fh,$_POST['data']);
fwrite($fh,$_POST['foo']);
fwrite($fh,$_POST["foo"]);
fwrite($fh,$_POST[foo]);
fclose($fh);
?>

but It doesn't work.The data is not wrriten to testFile.txt. I will appreciate your help. Thanks in advance.

dani
  • 11
  • 3

1 Answers1

0

No, JavaScript doesn't have access to writing files as this would be a huge security risk to say the least.

If you wanted to get/store information server-side, though, you can certainly make an Ajax call to a PHP/ASP/Python/etc. script that can then get/store the data in the server. If you meant store data on the client machine, this is impossible with JavaScript alone.

If you are only trying to store a small amount of information for an unreliable period of time regarding a specific user, I think you want cookies.

Updated:

Below is a simple code that you are looking for. There is a simple form with four fields. On clicking of submit, it calls the server file and that PHP file will have the code to write the data to a file.

$("#submit").click(function(){
  var paramsToSend = {};
  var i = 1;
  $("input[name='myanswer[]']").each(function(){
    paramsToSend[i] = $(this).val();
    i++;
  });
  
   $("#dataToSend").html(JSON.stringify(paramsToSend));
   
   $.ajax({
         type: "POST",
         url: 'URL_HERE/post.php',
         data: {params:JSON.stringify(paramsToSend)},
         success: function(data) {
          console.log("SUCCESS!!!");
         }
   });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inputsl">
   <div class="inputt"><input type="text" id="createmyanswer" name="myanswer[]" class="myinput"></div>
   <div class="inputt"><input type="text" id="createmyanswer" name="myanswer[]" class="myinput"></div>
   <div class="inputt"><input type="text" id="createmyanswer" name="myanswer[]" class="myinput"></div>
   <div class="inputt"><input type="text" id="createmyanswer" name="myanswer[]" class="myinput"></div>
</div>
<button id="submit">
Submit
</button>

<div id="dataToSend"></div>

PHP code can be:

file_put_contents('filename.txt', $_POST['params']);
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • Instead of cookies, you could also try [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API). – R. Schifini May 10 '17 at 12:01
  • I put "http://localhost:8012/myFolder/upload.php" in the url in the ajax call and put "testFile.txt" in the php file, but the content is not wrriten to the file. – dani May 10 '17 at 12:07
  • Can you explain what is not working? Is the ajax call not happening? Is the file writing not happening? – Milan Chheda May 10 '17 at 12:07
  • Did you tried `file_put_contents('filename.txt', $_POST['params']);`? Also, can you confirm if it has access to create a new file, in case the file doesnt exist. – Milan Chheda May 10 '17 at 12:09
  • both of them,The ajax call and the file writing – dani May 10 '17 at 12:09
  • If you need to write data to a text file using client side, you might want to check NodewebKit. Of course if it fits your plans. – Amiga500 May 10 '17 at 12:10
  • I need to write data using server side. – dani May 10 '17 at 12:11