CodeIgniter Forums
javascript files with php code - 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: javascript files with php code (/showthread.php?tid=63170)



javascript files with php code - Lykos22 - 10-02-2015

Hi guys, I'd like to ask for some generic information, if possible.

I usually try to put all my javascript stuff in a single file, and not keep them inside <skript> tags in my views, so it would be a little bit cleaner and tided up. However there might be some cases where I have ti embed some PHP code inside my javascript, for instance:
PHP Code:
<script type="text/javascript">
$(function() {
    $.
post('<?php echo base_url($url); ?>', function(data){
        
// some code here
    
});
});
</
script

So in this case I'm just leaving these scripts in my views.

I was wondering whether there's a more efficient way to do this, or another better way to organize all javascripts. Would you recommend another way, or in that case, or keep going this way ?


RE: javascript files with php code - davicotico - 10-02-2015

(10-02-2015, 09:02 AM)Lykos22 Wrote: Hi guys, I'd like to ask for some generic information, if possible.

I usually try to put all my javascript stuff in a single file, and not keep them inside <skript> tags in my views, so it would be a little bit cleaner and tided up. However there might be some cases where I have ti embed some PHP code inside my javascript, for instance:

PHP Code:
<script type="text/javascript">
$(function() {
 $.
post('<?php echo base_url($url); ?>', function(data){
 
// some code here
 
});
});
</
script

So in this case I'm just leaving these scripts in my views.

I was wondering whether there's a more efficient way to do this, or another better way to organize all javascripts. Would you recommend another way, or in that case, or keep going this way ?

You can use global variables in JS . Example:

Code:
<script type="text/javascript">
var URL = "<?php echo base_url() ?>"; //base_url is called just one time
$(function() {
$.post(URL+'register', function(data){ //and use the global var URL
// some code here
});
});
</script>



RE: javascript files with php code - freddy - 10-02-2015

yeah me too Big Grin

I love too use my js file with single file js not mixing them in php, based on experience i have done like this in view

Code:
<script type="text/javascript">
var products= "<?php echo base_url() ?>admin/products"; //consider it take data products
</script>




then in my js i use this
Code:
url : product,



RE: javascript files with php code - JayAdra - 10-02-2015

There are a few different ways to handle this - each have their pros and cons. It depends on preference and your specific requirements really.

This is a great post to refer to for information on each:
http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript

I generally do what others have suggested. I have a single <script> element in my <head> which contains all variables I need echoed into JS, which I can then reference anywhere in my other JS files. I use this mostly for language lines, but it has other uses too.


RE: javascript files with php code - Lykos22 - 10-15-2015

So basically, you do something like this:

Code:
<head>
<script>
      var url = "<?php echo base_url() ?>admin/products";
      // etc etc
</script>
</head>

<script src="path/to/some/file.js"></script>

// and then in your script in file.js you can access the url
url : url,
etc etc

Am I right ?

but what you do in cases like this, where you need to loop to set dynamically some js data ? See this example:
PHP Code:
'columns': [
       <?
php foreach($array_items as $item): ?>
        { 'data': '<?php echo $item?>' },
        <?php endforeach; ?>
],