最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Codeigniter base url didn't recognized in javascript - Stack Overflow

programmeradmin2浏览0评论

I have an ajax request in my CI application ,here is my cstom.js file for that purpose.

$(document).ready(function(){
    var base_url='<?php echo base_url();?>';
   $('#add-ct').click(function(){      
      $.ajax({
          url:base_url+'stockmanagement/add_category',
          data:{category:$('#category').val()},
          success:function(data){
              alert(data);
          },
          error:function(err){
              alert('error'+err);
          }
      });
   });
});  

But the base_url didn't recolonized, when i check the debug console the base_url is printed as the same as the source code like this way.

var base_url='<?php echo base_url(); ?>';

UPDATE

my javascript file is included in a view file

<?php if($page=='add-category'){ echo '<script src="'.base_url().'/assets/js/custom.js"></script>'; }?>

I have an ajax request in my CI application ,here is my cstom.js file for that purpose.

$(document).ready(function(){
    var base_url='<?php echo base_url();?>';
   $('#add-ct').click(function(){      
      $.ajax({
          url:base_url+'stockmanagement/add_category',
          data:{category:$('#category').val()},
          success:function(data){
              alert(data);
          },
          error:function(err){
              alert('error'+err);
          }
      });
   });
});  

But the base_url didn't recolonized, when i check the debug console the base_url is printed as the same as the source code like this way.

var base_url='<?php echo base_url(); ?>';

UPDATE

my javascript file is included in a view file

<?php if($page=='add-category'){ echo '<script src="'.base_url().'/assets/js/custom.js"></script>'; }?>
Share Improve this question edited Mar 27, 2019 at 2:28 Cœur 38.7k26 gold badges202 silver badges277 bronze badges asked Mar 17, 2016 at 4:18 Blessan KurienBlessan Kurien 1,6659 gold badges33 silver badges64 bronze badges 14
  • what is the file extension of your file? – Khairul Islam Commented Mar 17, 2016 at 4:22
  • its a js file custom.js – Blessan Kurien Commented Mar 17, 2016 at 4:23
  • how you expect that in a js file php will work? – Khairul Islam Commented Mar 17, 2016 at 4:24
  • the javascript should be located in your view file with the .php extension for it to recognize php code. – CodeGodie Commented Mar 17, 2016 at 4:26
  • where is this javascript located? provide the name of the file and the location of the file. – CodeGodie Commented Mar 17, 2016 at 4:27
 |  Show 9 more comments

5 Answers 5

Reset to default 14

The problem is that you are trying to run PHP code inside a JS file. This will not work as PHP code can only run inside files named with a .php extension.

To fix this, you have to set a global variable in Javascript to hold the value of your base_url

so if you want to include a JS file you should first define the variable like in the following example:

index.php

<html>
  <head>
    <!-- SET GLOBAL BASE URL -->
    <script>var base_url = '<?php echo base_url() ?>';</script>
    <script src="/assets/js/custom.js"></script>
  </head>
  <body>
  </body>
</html>

custom.js

//now we can reference the base_url
alert(base_url+"some/other");

Declare var base_url in your header file

<script>
var base_url = "<?php echo base_url();?>";
</script>

In the template, I put this :

<script>
  var base_url = "<?= base_url(); ?>";
</script>
<script src="<?= base_url('assets/js/main.js'); ?>">

And then in then main.js like this :

function logout(){
    var element = document.getElementById("lobtn");
    location.href = base_url+"user/logout";
}

It will send you to "yoursite.com/user/logout"...

Edit : The "lobtn" is an ID from your Logout Button...

<?php echo base_url();?>

The PHP code does not execute in js file. You have to declare var base_url = "<?php echo base_url();?>"; in a PHP file such as template file as a global variable.

<script>
    var base_url = "<?php echo base_url();?>";
</script>

You can do one thing copy above code and paste it to your header file

发布评论

评论列表(0)

  1. 暂无评论