• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass in a struct to javascript

#1
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>
Reply

#2
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
the simplest
change this line

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

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

#4
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);

    }
)
Reply

#5
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
Reply

#6
(06-05-2019, 07: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.
Reply

#7
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);
Reply

#8
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?
Reply

#9
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?
Reply

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

Return JSON response from AJAX using jQuery and PHP
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.