Organizational Research By

Surprising Reserch Topic

using external js with php extension in codeigniter


using external js with php extension in codeigniter  using -'php,javascript,codeigniter,fatal-error'

I am using codeigniter 2 and I have my theme folder consist of js, img, css folders.

Inside js folder, file name is js_functions.php contains:

<?php header("Content-type: text/javascript"); ?>
/**
 * GLOBAL VARIABLES & PATHS
 *
 * path definitions for jquery inline usage
 *
 */     
var base_url         = '<?=base_url();?>';
// ------------------------------------------------------------------------

/**
 * jquery.message Global Implementation
 *
 * Shows message if any session flashdata named message is set
 *
 */

<?php if($this->session->flashdata('message')):?>
$(function() { $().message("<?=$this->session->flashdata('message');?>"); });
<?php endif; ?>

// ------------------------------------------------------------------------


and, calling in it view file

<script type="text/javascript" src="<?=base_url();?>themes/admin/js/js_functions.php"></script>


renders correctly. But it returns in the chrome's inspect screen:

var base_url         = '<br />
Uncaught SyntaxError: Unexpected token ILLEGAL


and in the browser when you call the page from address bar:

var base_url         = '<br />
<b>Fatal error</b>:  Call to undefined function base_url() in <b>F:\xampp\htdocs\themes\js\js_functions.php</b> on line <b>11</b><br />


What's wrong? Isn't it the correct way of using php in external js/php file?
    
asked Sep 15, 2015 by ErickGallowa
0 votes
5 views



Related Hot Questions



Government Jobs Opening

answered Sep 15, 2015 by AlyLonon
0 votes

base_url() is a function part of the CI url helper. If you want to make use of it in that file and you don't want to create an instance of the $CI object, you will have to implement your own version of that function.

answered Sep 15, 2015 by DenMcDonagh
0 votes

If you want to generate dynamic js, css files. Use standart MVC method. Make a folder in view folder with name js or css. Create js or css files as php view file. For example : slider.js.php.

Make a controller themejs.php. Forward all js or css files to this controller which you need, by using routing. And inside this controller make dynamic action, pass varibles to js, css view files. And print them with header

answered Sep 15, 2015 by LatoyaNolenq

...