Welcome Guest, Not a member yet? Register   Sign In
How to pass in a struct to javascript
#11

(06-06-2019, 06:02 AM)richb201 Wrote: From the manual:

$data = array(
       'title' => 'My Title',
       'heading' => 'My Heading',
       'message' => 'My Message'
);

$this->load->view('blogview', $data);

I am doing this. But in the view (called 'javascript_funcs_extensionloaded.php', I can't see the $data or data. It is in <script> tags.

From the code in the first comment I am trying to just pass in "data", not "data.command".

I have tried everything from json_encoding to using cookies to just echoing to the html page. I can't seem to get the data across. Part of the issue is that I can't seem to get phpStorm to debug the js code in the view.  Can I use alert() in the view to display the variable?

You are doing it wrong. When you pass $data to your view, it creates new variables for each array member. The $data variable is not visible from the view. Read : https://codeigniter4.github.io/CodeIgnit...o-the-view

In this example, you will get the variables $title, $heading and $message.

Read the reply from hc-innov again. He already gave you the solution to pass data from PHP to JavaScript. Remember that PHP is executed on the server. The browser receive the html/js document generated by the server and only then the JavaScript code can be executed in the browser.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply


Messages In This Thread
RE: How to pass in a struct to javascript - by includebeer - 06-06-2019, 12:16 PM



Theme © iAndrew 2016 - Forum software by © MyBB