I just want to save JSON (generated using Javascript) in a file on the server. But I can't even get it to work with a simple line:
HTML file:
<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: "page.php",
data: {"foo": "bar"},
processData: false,
contentType: 'application/json'
});
});
});
</script>
</head>
<body>
<div id="test">
KLICK
</div>
</body>
</html>
And the php file looks something like this:
<?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);
Nothing succeeded. I also tried
$.ajax({
var datatosend="foo bar";
url: "page.php",
data: datatosend,
processData: false
});
I do not know what might be wrong. The txt file is located after clicking div in the html file. But there is no content in the file. If I just write $ _POST to a text file, the file contains the text "Array", which means that $ _POST has some content.
source
share