Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:03:47+00:00 2026-05-29T07:03:47+00:00

I am just wondering if it’s possible to use external JS file that contains

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:03:48+00:00Added an answer on May 29, 2026 at 7:03 am

    You can do it by either renaming you js file to php and using this link in script tag src (as pointed in other answers)

    While this may seems a good and easy way there is many reason not to do this.

    • Caching (your generated script will not be cached, this may be changed but require additional work)
    • Memory consumption (every request to this generated js file will require php)

    You can simply changed to something like this to get it done (in a better way, IMO):

    <script type="text/javascript">
      var Settings = {
        base_url: '<?= base_url() ?>',
        search_city: '<?= $_SESSION['search_city'] ?>'
      }
    </script>
    <script type="text/javascript" src="<?= base_url();?>js/external.js"></script>
    

    Than content of your js became something like this:

    $(document).ready(function(){
      // I assume that not using search_city in your sample code
      // and some syntax errors, is just by mistake...
      $('#update').click(function(){
      var tableVal={search_city:Settings.search_city};
      $.post(Settings.base_url+'/project_detail/pub',
        {'tableVal':tableVal},
        function(message){
          console.log(message);
        })
      })
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering whether it is possible to update a ByteArray in C Code, which
Just wondering, having the following simple code: var object1 = { name: function (){
Just wondering is it possible to build CLI app that can be run from
just wondering whether it's possible to change the screen timeout using code in Android
Just wondering if we can update an already built xml file using xml serialization/deserializtion
just wondering if there is a way to reduce the amount of code needed
Just wondering when do you actually use $.get(index); I am a bit confused on
Just wondering if anyone can optimize this usortMonths() function to be better? Basically I
Just wondering what it is that I am doing wrong here?? It's very simple
Just wondering. Say I have this in a php page <?php if x {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.