Jens
Jens

Reputation: 53

Save Data to File via Ajax POST and PHP

I just want to save some JSON (generated with Javascript) in a file on the server. But I even don't get it to work with a simple string:

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 is 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 worked. I also tried

$.ajax({
    var datatosend="foo bar";
    url: "page.php",
    data: datatosend,
    processData: false
    });

I don't know what could be wrong. The txt file is there after clicking the div in the html file. But there is no content in the file. If I just write $_POST to the text file, the file contains the Text "Array", meaning that $_POST has some content.

Upvotes: 5

Views: 18185

Answers (4)

Lix
Lix

Reputation: 48006

Few things could be going wrong here. Check the permissions on the directory your are trying to write to. Also make sure that your ajax call is using the POST method.

$.ajax({
  url: "page.php",
  type : 'POST',
  ...
}); 

As sated in the jQuery documentation, the type parameter sets the request type, POST or GET.

The type of request to make ("POST" or "GET"), default is "GET".


Another thing to consider is that you are not actually writing JSON data. The data is send in that format but when it gets to the $_POST variable it has been converted into an array. What you should try doing is writing a PHP array to the file -

$fh = fopen($myFile, 'w');
fwrite($fh,'<?php $arr='.var_export($_POST['data'],true).' ?>');
fclose($fh);

That should give a file similar to this -

<?php
  $arr = array(
    'foo'=>'bar'
  )
?>

As you can see, the var_export() function returns a parsable version of the variable.

var_export — Outputs or returns a parsable string representation of a variable

Upvotes: 2

skos
skos

Reputation: 4232

May be it should be

    $.ajax({
    url: "page.php",
    type: "POST", // add this
    data: {"foo": "bar"},
    processData: false,
    contentType: 'application/json'
});

OR

in PHP use $_REQUEST instead of $_POST

Upvotes: 0

user399666
user399666

Reputation: 19909

By default, all $.ajax() requests are sent using the GET method, not POST. You'll need to specify the type as 'POST':

$.ajax({
    var datatosend="foo bar";
    url: "page.php",
    type: 'post',
    data: datatosend,
    processData: false
    });

From the documentation:

type - String

Default: 'GET'

The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

Upvotes: 0

moonwave99
moonwave99

Reputation: 22820

You need to make a POST call, see below:

$('#test').click(function(){
    $.ajax({
        url: "page.php",
        type : 'post', 
        data: {"foo": "bar"},
        processData: false,
        contentType: 'application/json'
    });

});

Upvotes: 0

Related Questions