Welcome Guest, Not a member yet? Register   Sign In
Calling helpers from within external JS
#8

[eluser]Nick Husher[/eluser]
What about:
Code:
// mycontroller.php
// requires: PHP5
class MyController extends Controller {
  function index() {
    $some_array = array('one','two','threefourfive');
    $data['javascript_vars'] = array('arbitraryDataArray'=>$some_array);
    
    $data['javascript_vars'] = json_encode($data['javascript_vars']);

    $this->load->view('someview');
  }
}

// someview.php
<html>
   <head>
     <-script->
      var pageValues = &lt;?=$javascript_vars ?&gt;
     </-script->
   &lt;/head&gt;
   &lt;body&gt;
   &lt;/body&gt;
&lt;/html&gt;

You can access your PHP-defined Javascript array with pageValues.arbitraryDataArray.

It's not the best way of doing it, but it imposes a clear separation between Javascript logic code and 'bridge data' that communicates between your PHP and your Javascript.


Messages In This Thread
Calling helpers from within external JS - by El Forum - 02-18-2008, 10:30 AM
Calling helpers from within external JS - by El Forum - 02-19-2008, 02:13 PM
Calling helpers from within external JS - by El Forum - 02-20-2008, 12:33 AM
Calling helpers from within external JS - by El Forum - 02-20-2008, 03:32 PM
Calling helpers from within external JS - by El Forum - 02-20-2008, 03:55 PM
Calling helpers from within external JS - by El Forum - 02-20-2008, 04:24 PM
Calling helpers from within external JS - by El Forum - 02-20-2008, 04:42 PM
Calling helpers from within external JS - by El Forum - 02-21-2008, 11:30 AM



Theme © iAndrew 2016 - Forum software by © MyBB