问题描述:

I have a separate javascript file(buy.js) from my html file(pay.php) that the html file posts to once a form is submitted. However, there are some javascript variables in pay.php that are outside the scope of the form that I need to call in the separate javascript file (buy.js). Is this possible to do?

This is the snippet of javascript that I need to access in my html file (buy.js):

 var planTerm = term;

This is the javascript in the html that I need the separate javascript file to access (pay.php):

var term = 0;

var price = 0;

function updatePrice(e) {

price = parseFloat(select.value);

if (price == select.options[1].value){

term = 12;

} else if (price == select.options[2].value) {

term = 1;

}

}

Here is a Pastie of all the code.

网友答案:

Using PHP you can simply do (using the include() function):

include("File.php");

So, as my example goes,

The JS_INCLUDE.php File:

<html>
<body>
  <script>
    var JSvar = "This is a external JS var.";
  </script>
</body>
</html>

The index.php

<html>
<body>
    <?php
    include("JS_INCLUDE.php");
    ?>

    <script>
    document.write(JSvar);
    </script>
</body>
</html>

But of course if the .php file is remote from the directory, you cannot.

Sources:

Include() function: http://www.php.net/manual/en/function.include.php

相关阅读:
Top