0

I'm trying to echo my base url with my CI application in my main js file as so:

var url = "<?php echo base_url('association/') ?>" + $(this).attr("id");

and what I end up with is this:

<?php echo base_url('association/') ?>120

What am I doing wrong?

Jason Ayer
  • 575
  • 1
  • 8
  • 22
  • 2
    In javascript (.js) files you cannot execute php code. – Ravinder Reddy Jun 22 '16 at 14:21
  • 2
    unless configured otherwise, web servers do not run `*.js` files through the php interpretor – Steve Jun 22 '16 at 14:21
  • 1
    A better option is to output this data into the dom, either as data attribute, or in a script block, then retrieve it in your js – Steve Jun 22 '16 at 14:23
  • Deleted my answer, thanks @NicolasBouvrette is right and that's a decent solution with more detail than my own. – LeonardChallis Jun 22 '16 at 14:28
  • As a quick hack you *can* just rename the file something like `main.js.php` ... dirtier than a public toilet in Soho but it does work. – CD001 Jun 22 '16 at 14:36

0 Answers0