javascript - PHP base URL in external js file - Stack Overflow

I have few JS in HTML page, if I use this in HTML view file then its working fine, my page submit form

I have few JS in HTML page, if I use this in HTML view file then its working fine, my page submit form correctly without any issue.

But when I moved this JS codes to external JS file, then it shows error,

Below is my JS

$("#user_fm").submit(function (event) {
    event.preventDefault();
        $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>index.php/admin/peoples/add_user",
            data: $("#user_fm").serialize(),
-----

my problem is clear to me, that in view page this is easily get decoded to my url

"<?php echo base_url(); ?>index.php/admin/peoples/add_user"

but when i keep this in JS file it show like below in console,

POST http://localhost/center/index.php/admin/peoples/%3C?php%20echo%20base_url();%20?%3Eindex.php/admin/peoples/add_user

How can we put PHP codes in JS file?

I have few JS in HTML page, if I use this in HTML view file then its working fine, my page submit form correctly without any issue.

But when I moved this JS codes to external JS file, then it shows error,

Below is my JS

$("#user_fm").submit(function (event) {
    event.preventDefault();
        $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>index.php/admin/peoples/add_user",
            data: $("#user_fm").serialize(),
-----

my problem is clear to me, that in view page this is easily get decoded to my url

"<?php echo base_url(); ?>index.php/admin/peoples/add_user"

but when i keep this in JS file it show like below in console,

POST http://localhost/center/index.php/admin/peoples/%3C?php%20echo%20base_url();%20?%3Eindex.php/admin/peoples/add_user

How can we put PHP codes in JS file?

Share Improve this question edited May 18, 2015 at 12:37 SaidbakR 13.6k23 gold badges111 silver badges202 bronze badges asked May 18, 2015 at 12:18 rjcoderjcode 1,3471 gold badge27 silver badges64 bronze badges 4
  • 2 You can't place php code inside a js external file. – Masiorama Commented May 18, 2015 at 12:19
  • 1 You can make a global base_url variable in javascript and assign it manually. – Tony Commented May 18, 2015 at 12:20
  • you can also write in the view file like <input type="hidden" name="baseurl" id="baseurl" value="<?php echo base_url(); ?>"> in javascript baseurl = $('#baseurl').val(); – NIRANJAN S. Commented May 18, 2015 at 12:22
  • Note that you can refer to base url using a slash /. In JS, you can use location.host or location.hostname. – kosmos Commented May 18, 2015 at 12:23
Add a ment  | 

2 Answers 2

Reset to default 9

To be able to embed PHP in html or javascript code, like <?php echo base_url();?>, the file must have .php extension, while your external javascript file certainly has .js extension. Besides .js files are parsed by the browser, while PHP files must be executed on the server.

What you can do is to first define a variable in inline javascript, and then use it in code of external .js files. In index.php or whatever main file you use, place this before you use your external js code:

<script type="text/javascript">
var baseURL = "<?php echo base_url(); ?>";
</script>

And then in your external code:

$("#user_fm").submit(function (event) {
    event.preventDefault();
        $.ajax({
            type: "POST",
            url: baseURL + "index.php/admin/peoples/add_user",
            data: $("#user_fm").serialize(),
-----

clearly you cant access. Run your script with XXAMP YOU need to turn apache service on so that php can read YOUR base URL

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743931156a4531832.html

相关推荐

  • javascript - PHP base URL in external js file - Stack Overflow

    I have few JS in HTML page, if I use this in HTML view file then its working fine, my page submit form

    2天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信