Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
FNDN Admin Posted July 26, 2017 Posted July 26, 2017 Hello, I am trying to pass data from my PHP controller to a JavaScript controller. The data could be either a number or a JSON object. The best thing I can think of right now is to get the ID from the URL that the page loads the JS controller on and then use Ajax to retrieve the JSON object but if there is a better way, please let me know. Thanks
CodingJungle Posted July 26, 2017 Posted July 26, 2017 in your php controller do this: \IPS\Output::i()->jsVars['somekey'] = $myVar; this will add it as a JS variable to the <head> of the output.
FNDN Admin Posted July 26, 2017 Author Posted July 26, 2017 9 minutes ago, CodingJungle said: in your php controller do this: \IPS\Output::i()->jsVars['somekey'] = $myVar; this will add it as a JS variable to the <head> of the output. Exactly what I was looking for. Thank you!
Tom S. Posted July 26, 2017 Posted July 26, 2017 I don't follow. Do you mean you want to get the result of a php script and return it to your javascript script? If so, I would suggest using the jQuery post method. https://api.jquery.com/jquery.post/ The php data can be returned in JSON format. javascript like so: fucntion getMeThatSweetPHP() { var someVal = 1; $.post( "next_level_stuff.php", { val: someVal }, function( data ) { //DO COOL STUFF alert(data[0]+' '+data[1]); }, "json"); } PHP like so: <? $response[0] = $_POST['val'] + 2; $response[1] = 'IS THE MAGIC NUMBER!'; echo json_encode($response); ?> If that's not what you want, sorry...
FNDN Admin Posted July 26, 2017 Author Posted July 26, 2017 @Tom S. Thanks for the reply. That would also be an option if I used the ID. The PHP script pulls from the DB and either sends a cURL request to get the JSON object or makes a JSON object, but I can do that before loading output. Good info to have though for next time. Thanks
Recommended Posts
Archived
This topic is now archived and is closed to further replies.