-1

I know if I search this on google you get lots of hits back, and i have spent the last 2 days trying to figure out whats happening, and been unsuccessful.

So, I am using Wordpress, but not sure that has any relevance to my issue, and I have a valid JSON string in storage and I need it to be put in PHP and then an object.

The JSON string was an object in PHP which i converted because i needed it in sessionStorage, so here is how i converted it in PHP:

$encodedTime = json_encode($time);
$encodedTime = str_replace("'", "", $encodedTime);

This is then loaded into a element:

<a class="button" href="/?page_id=714" name="viewButton" rel='<?php echo $encodedTime ?>'>View</a>

Here is the object now stored into sessionStorage on the browser:

{"uuid":"108-417f-b5e6-970baec37","low":2929,"run":50793,"contact":"a07-e4b0-4977-ad10-af7aecf4ef80","completed":true,"values":[{"category":{"base":"Great","eng":"Great"},"node":"f85ed85-495e-8eaf-1e6cfdbadf97","time":"2015-10-12T11:28:13.371Z","text":"Great thanks","rule_value":"Great","value":"Great","label":"Response 1"}],"steps":[{"node":"83e332e0-a456-449f-a4f6-cee688f9395b","arrived_on":"2015-10-12T11:27:43.866Z","left_on":"2015-10-12T11:27:44.052Z","text":"Hello! Good Morning  This morning?","type":"A","value":"None"},{"node":"f85ed8a8-e745-495e-8eaf-1e6cfdbadf97","arrived_on":"2015-10-12T11:27:44.052Z","left_on":"2015-10-12T11:28:13.371Z","text":"Great thanks","type":"R","value":"Great"},{"node":"424912da-e680-432d-877c-307117366f98","arrived_on":"2015-10-12T11:28:13.374Z","left_on":"2015-10-12T11:28:13.477Z","text":"Thats great to hear!, Wishing you a good day","type":"A","value":"None"}],"created_on":"2015-10-12T11:27:43.721Z","modified_on":"2015-10-12T11:28:13.482Z","expires_on":null,"expired_on":null,"runtime":"12 - 10 @ 11 hour"}

This has been validated with JSONLint.

Here is the PHP code:

$time = "<script>document.write(time)</script>";

If i now print_r($time) I get the same printed, a valid JSON string. Great!

Based on what i have read PHP has json_decode for such a requirement, but it does not work, but it does give me a blank space

$time = json_decode("<script>document.write(time)</script>", true); // PHP Code
print_r($time);

If I try and json_encode it gives me funny characters.

If I try and make it an object using :

$time = (object) json_decode("<script>document.write(time)</script>", true);

It gives me :

stdClass Object ( )

$time = (object) "document.write(time)";

stdClass Object ( [scalar] => {"uuid":"1788-417f-b5e6-970bab44ec37","low":2629,"run":5046793,"contact":"a0eb0-4977-ad10-af7aecf4ef80","completed":true,"values":[{"category":{"base":"Great","eng":"Great"},"node":"f85ed85-495e-8eaf-1e6cfdbadf97","time":"2015-10-12T11:28:13.371Z","text":"Great thanks","rule_value":"Great","value":"Great","label":"Response 1"}],"steps":[{"node":"83e332e0-a45-a4f6-cee688f9395b","arrived_on":"2015-10-12T11:27:43.866Z","left_on":"2015-10-12T11:27:44.052Z","text":"Hello! morning?","type":"A","value":"None"},{"node":"f85ed8a8-5e-8eaf-1e6cfdbadf97","arrived_on":"2015-10-12T11:27:44.052Z","left_on":"2015-10-12T11:28:13.371Z","text":"Great thanks","type":"R","value":"Great"},{"node":"424912da-e680-432d-877c-307117366f98","arrived_on":"2015-10-12T11:28:13.374Z","left_on":"2015-10-12T11:28:13.477Z","text":"Thats great to hear!, Wishing you a good day","type":"A","value":"None"}],"created_on":"2015-10-12T11:27:43.721Z","modified_on":"2015-10-12T11:28:13.482Z","expires_on":null,"expired_on":null,"runtime":"12 - 10 @ 11 hour"} )

So I am at a loss, as to why it will not work. I have the option to use Javascript and use the information, but I really need it in PHP as there is more working to be done with this information.

Could someone please help point me where, and why, it's going wrong ?

Andrew Walker
  • 492
  • 1
  • 5
  • 23
  • 2
    php is executed before javascript so the php code is not seeing the output from the javascript `document.write` – Professor Abronsius Oct 18 '15 at 08:57
  • @RamRaider how would I get the timing right ? – Andrew Walker Oct 18 '15 at 08:59
  • send the JSON object via ajax to a php script – Professor Abronsius Oct 18 '15 at 09:02
  • So it is possible to link to a page, using HTML, while at the same time Ajax in Javascript to the same page it's linking too? – Andrew Walker Oct 18 '15 at 09:06
  • Apart from all that: why on earth that line of code: `$encodedTime = str_replace("'", "", $encodedTime);`? You modify your json string such that it might not be valid json any more! – arkascha Oct 18 '15 at 09:11
  • @arkascha Hiya, as you can read, it is valid after I take out the `'` and the reason is because there could be text, and is in this example, of them being included. I was unable to decode the whole object because of them. – Andrew Walker Oct 18 '15 at 09:48
  • @AndrewWalker Sorry, I cannot really make any sense of your last comment. – arkascha Oct 18 '15 at 11:38
  • @arkascha So you mark me down, that makes even less sense. There is a string with a ' in it, when i added this encode json into an element, it was confused by it and cut part of the json off, and then it did not make a valid json. I did put above in my first very detailed explanation of this. There was no reason to make it down. Only my comment if you felt it was lacking! – Andrew Walker Oct 18 '15 at 13:56
  • @AndrewWalker I did _not_ "vote you down". All I mentioned was that your last comment above is impossible to understand. I mean read it yourself...About your issue: I doubt very much that "json got confused" about a ' in a string. There certainly is some issue you experience, I do not doubt that. But wild pointing and replacing arbitrary characters in a random manner will not solve it, except maybe by accident. So sorry that I could not give a clear answer, such is only possible if the one who questions writes in a manner one can understand. Anyway: as said it was not me who voted this down... – arkascha Oct 18 '15 at 14:09

1 Answers1

1

You can't get a javascript variable value using PHP code. They're differente things. If you want to understand more, i recommend you read this question.

So... How to do it?

If you wanna use that data in the back-end, you need to create a AJAX request, as RambRaider said. If you need this in the front-end, you can simply parse the json data using JSON.parse.

Community
  • 1
  • 1
Iago
  • 1,214
  • 1
  • 10
  • 19
  • Thank you lago. As I stated I could not use javascript as there was still much I needed to do. It was just one, and the first, of five where different parts of this is used in other functions. I did manage to json.parse it, and it was successful :) – Andrew Walker Oct 18 '15 at 14:01