0

Objective: Send the JSON string that resides in localStorage to a text file at the server.

I tried using AJAX and JQuery. Link1 Link2

My code:

JAVA Script

function testAPI() {
                FB.api('/me?fields=id,name,email', function(response) {
                    localStorage.user_data=JSON.stringify(response);

                        $.ajax({
                                    type: "POST",
                                url: "https://vtpt.in/~ad/ignore/backend.php",
                            data: {localStorage.user_data},
                        success: function() {alert("Data Sent");}
                    });


                });
    }

PHP

Below is the data retrieved from SERVER

<?php
    $dataObject = $_POST['data']; //Fetching all posts
    file_put_contents('user_data.txt', $dataObject , FILE_APPEND | LOCK_EX );

?>
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71

2 Answers2

0
$dataObject = $_POST['data']; //Fetching all posts
var_dump($_POST);//Verify you get the content
$result = file_put_contents('user_data.txt', $dataObject , FILE_APPEND | LOCK_EX );
if (!$result) {
    var_dump(error_get_last());//prints last php last error
}
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
  • No luck. Can you please check if the Syntax(s) I have used are correct including data format that have to be Transferred. I have doubts about them. Why is it so difficult to Store a single Text file on a Server !! file_put_contents should at least create a new file, what do you say? – ankush dubey Dec 22 '17 at 07:57
  • Here's an update, JAVA Script: var data_throw= localStorage.user_data; then in the ajax section, I did, data: {data_throw}... And now as you would see, alert is given that data is sent to the Location BUT still I am not able to see the where the Data is lending. – ankush dubey Dec 22 '17 at 08:04
  • what do you get on `var_dump($_POST)` ? – Thamaraiselvam Dec 23 '17 at 07:50
0

The issue was that My server did not have PHP installed. Installing PHP removed the issue that I wasn't getting changes at the File System level i.e. no file was created. But works fine Now.