0

I am building a platform where users can draw graphs. In order to do this, I am using Chart.js and each time a graph is created, I save an instance (a javascript object) of this one in a javascript array.

What I would like to do is to save that array of instances in a PHP session variable with AJAX, so that the user can retrieve his configuration later on.

From what I understood, the Javascript object sent to PHP via AJAX will be converted to PHP array format and the returned value will depend on the "dataType" that we expect (json, html, xml...)

However, what I want to retrieve is the exact same Javascript array of my graphs instances in order to re-instantiate them, not a "converted format".

Thank you in advance for your help !

Eric
  • 11
  • 1
  • 4
  • 1
    If you want to be absolutely sure, that you get the same object back from PHP (no conversion what so ever), you can serialize it as a string: `JSON.stringify(object)`. – ssc-hrep3 Jun 20 '16 at 09:47
  • I agree with @ssc-hrep3 and one more suggestion please attach some code or fiddle for better clarification. – Haresh Kumar Jun 20 '16 at 09:52

2 Answers2

0

You have few options:

  1. AJAX + PHP session + generate stuff with php (pure js, cookies, localStorage)
    • session ends when browser is closed
    • much codewritting
  2. localStorage
    • storage gets wiped when browser is closed
    • best safety and easiest
  3. Cookies
    • you have expiration date
    • you need to provide message about using cookies
    • cookies can be generated with PHP and with JS
  4. You can provide links/codes which generates your graphs. I do it, user just send link to his mail, and he can recreate stuff any time.
RaV
  • 1,029
  • 1
  • 9
  • 25
  • I think he already decided for `1.` and wanted to know how to store the data in the PHP session variable. – ssc-hrep3 Jun 20 '16 at 09:56
  • Sure, `session_start(); $_SESSION["your_variable"]=$_GET['your_variable'];` – RaV Jun 20 '16 at 09:59
  • And with AJAX you need to post url like http://yourpage.com/ajax.php?your_variable=generate_my_chart – RaV Jun 20 '16 at 10:01
0

If you want to be absolutely sure, that you get the same object back from PHP (no conversion what so ever), you can serialize it as a string (JSON.stringify(object)) and afterwards parse it again.

Here is one solution:

JavaScript (send data):

var data = [
    { "abc": "def" },
    { "ghi": "jkl" }
];

$.ajax({
    "url": "http://localhost/savedata.php",
    "method": "POST",
    "data": {
        "list": JSON.stringify(data)
    }
});

PHP (savedata.php):

<?php
    session_start();

    $_SESSION['data'] = $_POST['list'];
 ?>

PHP (getdata.php)

<?php
    session_start();
    header("Content-Type: application/json");

    echo $_SESSION['data'];
?>

JavaScript (receive data):

var data = [];

$.ajax({
    "url": "http://localhost/getdata.php",
    "method": "GET",
    "success": function(response) {
        data = response;
    }
});
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87