Jump to content

Pass data to JavaScript controller


FNDN Admin

Recommended Posts

Posted

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

Posted
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!

Posted

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...

Posted

@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

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...