0

I got data from third party plugin and I just got [object Object] in javascript. I want to pass it on PHP and show all the values on that [object Object]. How could I to do that ? I already pass it to PHP through GET

I got this [object Object] from this part.

widget.createButton()
    .attr('title', "Save chart")
    .on('click', function (e) {
    widget.save(function(data) {
        window.location.href = "temp.php?var=" + data;
    })
})
.append($('<span>save chart</span>'));

This is my PHP code.

<?php

$var = $_GET['var'];

var_dump($var);
exit();

?>

if I do var_dump it still get [object Object] but I want to get the values of that [object Object].

Antonio
  • 755
  • 4
  • 14
  • 35

1 Answers1

0

Your object is being converted to a string. Try this in a browser console to see.

var a = {}
a.toString() // this will give you [object Object]

In your code at this step:

window.location.href = "temp.php?var=" + data;

you are converting data from object to string. You need to do this

window.location.href = "temp.php?var=" + JSON.stringify(data);

and on PHP part of the code, you will now receive it as string, so you need to do this:

$var = var_dump(json_decode($_GET['var']));
Amresh Venugopal
  • 9,299
  • 5
  • 38
  • 52
  • if I convert it from `object` to `string` with `JSON.stringify(data)` the URI of `temp.php?var=" + JSON.stringify(data)` is too large. – Antonio Feb 22 '17 at 04:50
  • because of that I want convert it in `PHP` after it pass through `GET`. – Antonio Feb 22 '17 at 04:51
  • Then you shouldn't use a get request for it. What you need to understand is, `[object Object]` no longer contains your actual object data and cannot be converted back. Just look at the first code snippet, even `{}` looks like `[object Object]` it is just a way of saying an object got converted to a string. – Amresh Venugopal Feb 22 '17 at 04:53
  • @Antonio http://stackoverflow.com/questions/2659952/maximum-length-of-http-get-request please refer to this to see why you can't send more data. – Amresh Venugopal Feb 22 '17 at 04:54
  • and what should I do to pass it if `GET` cannot pass that data ? – Antonio Feb 22 '17 at 04:56
  • use https://api.jquery.com/jquery.post/ or http://www.openjs.com/articles/ajax_xmlhttp_using_post.php to make a `POST` request. – Amresh Venugopal Feb 22 '17 at 04:58