-1

I have a page (insert.php) from which I am trying to return a response that contains $my_variable as a json array back to another page (form.php).

I can see this array in the response section of my firebug console but how can I get at it on form.php and use it with php?

insert.php is sending this:

// sending output
header('Content-Type: text/json');
echo json_encode(array("my_var" => "$my_variable"));
}
JulianJ
  • 1,259
  • 3
  • 22
  • 52
  • You're seeing that JSON array where? As response to an AJAX call? From Javascript? Then *Javascript* will have to do something with it, not PHP. This isn't very clear and I'm pretty certain you have some fundamental confusion as to what's what, so I'll close this as duplicate of something that may help you get on the right track. – deceze Mar 09 '16 at 13:42

1 Answers1

0

just use json_decode function

$arr = json_decode($_REQUEST['my_var'],true);
LightNight
  • 851
  • 1
  • 6
  • 14
  • 1
    1) Why would this value be in the current `$_REQUEST`, and 2) why would PHP be able to automatically decode it from JSON into `my_var`? – deceze Mar 09 '16 at 13:44
  • if I understood you correctly you are posting value of my_var on form.php, and to handle operation what you mentioned in post you must use json_decode to get back array – LightNight Mar 09 '16 at 13:49
  • I am actually posting data from a form on form.php with ajax to insert.php where the data is used in some maths. I now want to return the result of the maths back to form.php as a variable which I can use with php on the page. – JulianJ Mar 09 '16 at 14:11
  • @JulianJ Well, that fundamentally won't work. – deceze Mar 09 '16 at 14:16