I am just wondering if it’s possible to use external JS file that contains PHP code.
my external JS
$(document).ready(function(){
$('#update').click(function(){
var tableVal={};
// a bit of php code I need in JS
var search_city=<?php echo $_SESSION['search_city'];?>';
$.post('<?=base_url()?>/project_detail/pub', {'tableVal':tableVal},function(message)
})
})
})
my view page
<script type="text/javascript" src="<?= base_url();?>js/external.js"></script>
The JS doesn’t work as I assume that the PHP code in JS is the problem. Any thoughts? Thanks a lot.
You can do it by either renaming you
jsfile tophpand using this link inscripttagsrc(as pointed in other answers)While this may seems a good and easy way there is many reason not to do this.
jsfile will requirephp)You can simply changed to something like this to get it done (in a better way, IMO):
Than content of your
jsbecame something like this: