How to pass in a struct to javascript |
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>
proof that an old dog can learn new tricks
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 )
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); } )
proof that an old dog can learn new tricks
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 (06-05-2019, 06:10 AM)hc-innov Wrote: Sorry I made a mistake in my code. 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.
proof that an old dog can learn new tricks
in your controller
PHP Code: $data['forjavascript'] = json_encode(array('message'=>'mymessage','command'=>'mycommand')); in your javascript: Code: var mydata = <?php echo $forjavascript; ?>;
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?
proof that an old dog can learn new tricks
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?
proof that an old dog can learn new tricks
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 )
|
Welcome Guest, Not a member yet? Register Sign In |