Welcome Guest, Not a member yet? Register   Sign In
trying to call javascript
#11

If your web browser is running then just hit F12 key it will open the browsers development tools.

You can see what is happening with javascript and ajax calls in the console.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#12

Typically, Javascript is not called from the controller (unless it's an AJAX call), but inside a view.
To load an external .js file, you need to use the base_url() function.
E.g., if your js files are in the /assets/js folder, you load it like this (in your view):
Code:
<script src="<?= base_url('assets/js/example.js');?>" />
After this, the functions in your js file are available.
You can save yourself a lot of trouble if you load the jQuery library. It can do a lot of dynamic html actions for you, like showing or hiding parts of your page. It can also simplify AJAX calls.

Good luck.
Reply
#13

(05-10-2019, 06:04 AM)richb201 Wrote: 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? 

Yes you are missing somthing big. You will have a hard time doing this if you don't start from the begining: learn how JavaScript works with plain HTML, how it interact with the browser and the server. Then, learn how to integrate your JavaScript code in a PHP application.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#14

(This post was last modified: 05-10-2019, 05:51 PM by richb201.)

(05-10-2019, 11:02 AM)includebeer Wrote:
(05-10-2019, 06:04 AM)richb201 Wrote: 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? 

Yes you are missing somthing big. You will have a hard time doing this if you don't start from the begining: learn how JavaScript works with plain HTML, how it interact with the browser and the server. Then, learn how to integrate your JavaScript code in a PHP application.

Thanks. This is the part I am currently missing:
>>Then, learn how to integrate your JavaScript code in a PHP application.

This is the part I am asking about.  Your comment reminds me of some comments I have seen about playing jazz guitar. They go like this: the person asks how do I add some jazz licks to my blues playing. The answer goes like this:

1) first learn all the notes on the neck
2) then learn all the scales
3) then learn all the chords in all the positions
4) then learn the arpeggios of all chords in 3 positions
5) learn to sight read

I know there are some guitar players up here who have seen this type of comment on the guitar websites. As a "functional" jazz guitarist I can tell you that it would take years and years to cover all that stuff. I have been playing for 45 years and know some of them, not all of them. Could I tell a newbie guitar player how to do it? No but I could point them in the right direction. I would tell them about "playing the changes". That is the crux of it. It is not the answer, but it is the "path" to success. I'd tell them about articles or books that cover that topic. 

If I am a little short, it is just that I am getting frustrated. Sorry. 

I am getting this response: Uncaught ReferenceError: register_extension is not defined

register_extension() calls my sendMessage()If I could get past the CI interaction with JS, I am sure I could get the messaging working on my own. I am pretty good with a debugger.  
proof that an old dog can learn new tricks
Reply
#15

Alright, I don't know what your experience is with CodeIgniter but I'll try to guide you in the right direction. 

You're trying to load a JavaScript file and call a function. Since JavaScript code is executed by the browser, you need to do this in the View. 

It is a best practice to separate logic and presentation. So what I like to do, is build an array in the Controller of all the JS and CSS file I will need, and output these in the view. Here is a really simple example of that:
PHP Code:
$data = array(
 
 'js' => array('jQuery.js''myApp.js'),
 
 'css' => array('bootstrap.css''myApp.css',
 
 'page_title' => 'JS Example')
);
$this->load->view('myView'$data); 


In the View, you read these arrays and output the filenames to the final HTML document so the browser can load them. Then you can execute any JS function present in these scripts.
PHP Code:
<?php foreach($js as $filename): ?>
<script type="text/javascript" src="<?php echo $filename?>"></script>
<?php endforeach; ?>

<script type="text/javascript">
  doSomething();
</script>

<h1><?php echo $page_title?></h1> 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#16

A Beginner’s Guide to AJAX With jQuery

Webucator's Free Ajax Tutorial

jQuery and Ajax (not for the dummies)
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#17

(05-11-2019, 08:18 AM)InsiteFX Wrote: A Beginner’s Guide to AJAX With jQuery

Webucator's Free Ajax Tutorial

jQuery and Ajax (not for the dummies)

Did I missed something? Where did he say he wants to do an ajax request?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB