Jump to content

Pass data to JavaScript controller

FNDN Admin

Recommended Posts


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.



Link to comment
Share on other sites

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.


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

Link to comment
Share on other sites

@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

Link to comment
Share on other sites


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

  • Recently Browsing   0 members

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