CodeIgniter Forums

Full Version: trying to call javascript
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I realize that js is really a client thing and not a server thing. Anyway, I am trying to call 


Code:
$(register_extension(
   var editorExtensionId = "mebiphcpglaighebopfapfoiimgnnbpf";

   // Make a simple request:
   chrome.runtime.sendMessage(editorExtensionId, "snapshot_mode",
       function(response) {
           if (!response.success)
                  handleError(url);

})
I try to load it with 

PHP Code:
$this->load->js("/js/javascript_funcs_extensionloaded.js");
javascript_funcs_extensionloaded(); 
I am getting:

A PHP Error was encountered

Severity: Error

Message: Call to undefined function javascript_funcs_extensionloaded()

Filename: controllers/Configure.php
Line Number: 43

Can anyone tell me how to look at this? 
@richb201,

Are you trying to have your web app send a browser message?
(05-07-2019, 07:03 PM)php_rocs Wrote: [ -> ]@richb201,

Are you trying to have your web app send a browser message?

Not really. I am trying to have the browser page send a message to the extension of the browser. There is some messaging facility in chrome. So I want the web page (which was built by the server), to make a call to the js which will send the message.

Or is that what you meant? I did ask a similar thing recently, but I am just trying to get the js to run on a page created by grocery crud or code igniter.

My overall plan is to call the aws javascript api from the extension. But I need to communicate some instructions from CI created browser page before I do that. People say that the Extension "runs in the context of the browser". I am not sure what that means, honestly, other than I can use some chrome runtime messaging feature (see the code above). But the only way I can communicate using the Chrome messaging app is to call a js function from my php created web page.

I guess the js needs to be embedded in the html but I don't know how to do that. Is the problem that I need to call register_extension() from the CI code? How does CI know I am calling a js function?
I changed the function call in my php program to

 $this->load->js("/js/javascript_funcs_extensionloaded.js");
 register_extension();


I am getting error: call to undefined function register_extension()

Now I tried:

echo "<script> register_extension(); </script>";

but need to put a debugger on the extension to see if it actually ran. 
Try adding the function to a var

Code:
/**
* Document   : web-app.js
* Author     : aka (InsiteFX)
*
* save as : js/web-app.js
*/

var webApp = function() {
    
    /* Scroll to top functionality */
    var scrollToTop = function() {

        // Get link
        var link = $('#to-top');

        $(window).scroll(function() {
            // If scrolled 150 pixels show the link
            if ($(this).scrollTop() > 150) {
                link.fadeIn(100);
            } else {
                link.fadeOut(100);
            }
        });

        // On click go to top of page
        link.click(function() {
            $('html, body').animate({scrollTop: 0}, 200);
            return false;
        });
    };
    
    // Initialize all functions here by name...
    return {
        init: function() {
            // Scroll to top (var scrollToTop = function(){})
            scrollToTop();
            
            // add other functions below here.
        }
    };

}();

// Initialize WebApp when page first loads
$(function() {
    webApp.init();
});
Thanks. How do I call it from my controller? I tried calling it from

echo "<script> webApp(); </script>";


I am getting these errors in my xdebug window:

Uncaught ReferenceError: webApp is not defined
Uncaught ReferenceError: $ is not defined
Uncaught SyntaxError: Unexpected token var
It already loads all the functions see the init function, then at the bottom you
can see that it loads itself.

The only way that I know how it to check the page post value then send an ajax call.

Code:
$(document).ready(function(){
   $("#button").on('click',function(){
           var name = $('#name').val();
           var email= $('#email').val();
           var data = 'name=' + name + '&email=' + email;
           $.ajax({
               url: 'login_processor.php',
               type: "POST",
               data: data,
               cache: false,
               dataType: "JSON",
               success: function(data){
                   if(data.error_name){
                       alert(data.error_name);
                       return false;
                   }
                   if(data.error_email){
                       alert(data.error_email);
                       return false;
                   }
               }
           });
       });
   });
//login_processor.php
   $error_name = null;
   $error_email = null;
   if(empty($_POST['name'])){
       $error_name = 'Please Enter Name!';
   }
   if(empty($_POST['email'])){
       $error_email = 'Please Enter Email!';
   }
   echo json_encode(array('error_name'=>$error_name, 'error_email' => $error_email));

Something like that you need to set it up for your stuff.

The url would point to your controller method.
Thanks insite. What I really need is a document written for beginners trying to call javascript from a php generated html page. I understand the xmlhttp stuff. My extension uses that to communicate back to my server. Works great, after much pain.

I just need my web app to initiate a message to my Extension. I control bought sides. My plan is to write a .png to aws S3 directly from the extension. I could send the .png to my server and have my server upload it to S3 in php, but that would be wasteful of bandwidth, which costs $.

So if you know how I can simply get my javascript function to be called from my controller, I will work out the messaging details.
You can try this one out to see if it will work for you.

Code:
<html>
<head>
    <title>Test</title>
    <script src="test.js" type="text/javascript"></script>
</head>
<body>
<?php
    echo '<script type="text/javascript">someFunction();</script>';
?>
</body>
</html>

You can also try to echo it from your controller.
I changed the call in my controller to:

         $this->load->js("/js/javascript_funcs_extensionloaded.js");
         echo '<script type="text/javascript" > register_extension(); </script>';

I just can't tell if it is running. I put a breakpoint in my js register_extension module, but phpStorm is not stopping there. I think  my xdebug is set correctly. It seems that I don't know how to declare a javascript function in my php program. I found this is the documentation re:Javascript Class

"This library is DEPRECATED and should not be used. It has always been with an ‘experimental’ status and is now no longer supported. Currently only kept for backwards compatibility."

OK, so it was taken out Undecided.  But what do the rest of us do now?

I am getting these errors in my console:

index:1

Uncaught ReferenceError: register_extension is not defined

javascript_funcs.js:1

Uncaught ReferenceError: $ is not defined

http://localhost/sub_crud/js/javascript_funcs_extensionloaded.js:2

Uncaught SyntaxError: Unexpected token var

index

Here is the javascript

$(register_extension(
   var editorExtensionId = "lamacgnkfoiknjpleghfknfigbmhdaei";

   // Make a simple request:
   chrome.runtime.sendMessage(editorExtensionId, "snapshot_mode",
       function(response) {
           if (!response.success)
                  handleError(url);

})

)

There seems to be something BIG I am missing here? It would seem that I would need to tell the PHP program that register_extension() is a valid javascript function? 
Pages: 1 2