CodeIgniter Forums
How to pass in a struct to javascript - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to pass in a struct to javascript (/showthread.php?tid=73783)

Pages: 1 2


How to pass in a struct to javascript - richb201 - 06-04-2019

I am trying to pass a few strings into a js function, which then passes it to my Extension. It is not working. How do I pass the data struct in?
Here is how I am calling it:
$data = array(
   'command' => 'snapshot_mode1',
   'message' => 'My Message'
);

$this->load->view("javascript_funcs_extensionloaded",$data);

I am not sure if "snapshot_mode1" it is getting into the js function. What is wrong with this? 

Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script language="javascript">


   $(document).ready( function(data) {
       var editorExtensionId = "lamacgnkfoiknjpleghfknfigbmhdaei";
       // unload when done
       $(window).on('beforeunload',(function(){
           chrome.runtime.sendMessage(editorExtensionId, "snapshot_mode_unload")
       }))


       chrome.runtime.sendMessage(editorExtensionId, data.command,
      // chrome.runtime.sendMessage(editorExtensionId, "snapshot_mode3",
           function (response) {
               if (!response.success)
                   handleError(url);

           }
       )

       })


</script>



RE: How to pass in a struct to javascript - InsiteFX - 06-04-2019

You need to pass the data to javascript using json so json_encode your array
before sending it javascript.

You may also need to use an ajax get command to get the data from php.


RE: How to pass in a struct to javascript - hc-innov - 06-05-2019

the simplest
change this line

Code:
chrome.runtime.sendMessage(editorExtensionId, data.command,

to
Code:
chrome.runtime.sendMessage(editorExtensionId, <?php echo '"'.$data['command'].'"'; ?>,



RE: How to pass in a struct to javascript - richb201 - 06-05-2019

I tried both of these and neither worked. One problem I have is my inability to see what is getting passed to the javascript function. I am using phpStorm and can't seem to put a breakpoint in the view.

I tried this: $this->load->view("javascript_funcs_extensionloaded", json_encode($data));

and also this:

chrome.runtime.sendMessage(editorExtensionId, <?php echo '"'.$data['command'].'"'; ?>,
// chrome.runtime.sendMessage(editorExtensionId, "snapshot_mode3",
    function (response) {
        if (!response.success)
            handleError(url);

    }
)



RE: How to pass in a struct to javascript - hc-innov - 06-05-2019

Sorry I made a mistake in my code.
Replace with:

Code:
chrome.runtime.sendMessage(editorExtensionId, <?php echo '"'.$command.'"'; ?>,

Don't use jsonencode in your controller


RE: How to pass in a struct to javascript - richb201 - 06-05-2019

(06-05-2019, 06:10 AM)hc-innov Wrote: Sorry I made a mistake in my code.
Replace with:

Code:
chrome.runtime.sendMessage(editorExtensionId, <?php echo '"'.$command.'"'; ?>,

Don't use jsonencode in your controller

The thing is that I  need to send more than just the 'command' string. I have the struct named $data which has both a "command" string and a "message" string.  So that is why I tried $data.


RE: How to pass in a struct to javascript - hc-innov - 06-05-2019

in your controller

PHP Code:
$data['forjavascript'] = json_encode(array('message'=>'mymessage','command'=>'mycommand')); 

in your javascript:

Code:
var mydata = <?php echo $forjavascript; ?>;
//mydata is a javascript object at this point see:
console.log(mydata);
// You can use it as you want, for example:
console.log(mydata.message);
console.log(mydata.command);



RE: How to pass in a struct to javascript - richb201 - 06-05-2019

I am getting "Use of undefined constant mydata" from the code below:

in php controller:
$this->load->view("javascript_funcs_extensionloaded", json_encode($data));

in view (this has javascript tags):
var mydata = <?php echo $data; ?>;
chrome.runtime.sendMessage(editorExtensionId, <?php echo '"'.mydata.'"'; ?>,

Q? is $data a global variable in php?


RE: How to pass in a struct to javascript - richb201 - 06-06-2019

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?


RE: How to pass in a struct to javascript - InsiteFX - 06-06-2019

This will show you how to pass data in json format to a jQuery ajax.

Return JSON response from AJAX using jQuery and PHP