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 6087753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:54:22+00:00 2026-05-23T11:54:22+00:00

Basically, the problem is this: <?php $test=foobar; ?> If within the html document I

  • 0

Basically, the problem is this:

<?php $test="foobar"; ?>

If within the html document I call

<script type="text/javascript">alert("<?php echo $test; ?>")</script>

, everything is fine.

However, if I do the same thing in an external jS document included with

<script type="text/javascript" src="foo.js"></script> 

it does not work.

Is there any way to do this?

  • 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-23T11:54:23+00:00Added an answer on May 23, 2026 at 11:54 am

    In the first case you actually pass the value to the PHP script, where <?php echo $test; ?> is parsed by PHP parser and replaced by the value of $test PHP variable.

    In the second case you just include ‘<?php echo $test; ?>‘ string into JS file and this is not parsed.

    My suggestion is: pass this value the way you are passing now, but properly reference it from the external JS file. For example you can invoke some function defined in JS file, passing the parameter from PHP file.

    There is also the ugly way of doing that, such as making your server treat JS files as PHP files, but I am not recommending it.

    Some example:

    If you include in your PHP file:

    <script type="text/javascript" src="foo.js"></script>
    <script type="text/javascript">myHappyFunction("<?php echo $test; ?>")</script>
    

    and in your JS file (foo.js):

    (function(){
        var myHappyFunction = function(myParameter){
            alert(myParameter);
        };
        window['myHappyFunction'] = myHappyFunction; // pass outside scope
    })();
    

    you should see $test value being alerted when the script executes (assuming I did not make a mistake writing this ;)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I only see PHP solutions to this problem. Basically I need to go from:
The problem is basically this, in python's gobject and gtk bindings. Assume we have
I have basically the same problem outlined in this question, however I am using
Sorry for the vague title, as I really can't explain this problem succinctly. Basically
This is basically a math problem, but very programing related: if I have 1
Basically my problem is with the page at http://wiki.diablocommunity.com/index.php?title=Tristram_Cathedral Please see the screen shots
I have a problem in PHP. I am using <input type=file> to get the
I have this code basis: echo '<input required type = text name = subject1
Please review this Stackoverflow post. I have the same PHP problem as bob_cobb. Here's
Scenario: I have a php page in which I call a python script. Python

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.