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

How to define a variable in JavaScript with PHP echo function? - Stack Overflow

programmeradmin3浏览0评论

How do I define a variable in javascript with echo function, from the external php file?

We have theconfigfile.php, thejsfile.js and thephpfile.php.

In theconfigfile.php we have:

<?php
$path = '.php'; // Set your path
?>

In thejsfile.js we have:

... 
if (confirm("Are you sure you want to delete")) {
        $.ajax({
            type: "POST",
            url: ".php",
            data: dataString,
            cache: false
        });
...

And in thephpfile.php we have:

    <php 
    include "theconfigfile.php";
    ?>

    <html>
    <head>
    <script type="text/javascript" src="thejsfile.js"></script>
    </head>

<body>
...here is the code that uses file thejsfile.js...
</body>
</html>

I used this method:

... 
if (confirm("Are you sure you want to delete")) {
        $.ajax({
            type: "POST",
            url: "<?php echo $path; ?>",
            data: dataString,
            cache: false
        });
...

Only works when javascript is part of the code. And if I use it external, like this...

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

...does not work! Solutions?

How do I define a variable in javascript with echo function, from the external php file?

We have theconfigfile.php, thejsfile.js and thephpfile.php.

In theconfigfile.php we have:

<?php
$path = 'http://example./home.php'; // Set your path
?>

In thejsfile.js we have:

... 
if (confirm("Are you sure you want to delete")) {
        $.ajax({
            type: "POST",
            url: "http://example./home.php",
            data: dataString,
            cache: false
        });
...

And in thephpfile.php we have:

    <php 
    include "theconfigfile.php";
    ?>

    <html>
    <head>
    <script type="text/javascript" src="thejsfile.js"></script>
    </head>

<body>
...here is the code that uses file thejsfile.js...
</body>
</html>

I used this method:

... 
if (confirm("Are you sure you want to delete")) {
        $.ajax({
            type: "POST",
            url: "<?php echo $path; ?>",
            data: dataString,
            cache: false
        });
...

Only works when javascript is part of the code. And if I use it external, like this...

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

...does not work! Solutions?

Share Improve this question edited Dec 6, 2012 at 8:14 j0k 22.8k28 gold badges81 silver badges90 bronze badges asked Dec 5, 2012 at 23:21 user1880576user1880576 3
  • Why would you want to echo the path via php? Why not just do it in javascript by hard coding the url, or assigning the path into a variable for later use? – Dennis Martinez Commented Dec 5, 2012 at 23:24
  • Using ajax with inline php seems contradictory, not the best idea IMO. – elclanrs Commented Dec 5, 2012 at 23:26
  • Because i have many files, and i want to modify from one file. – user1880576 Commented Dec 5, 2012 at 23:26
Add a ment  | 

7 Answers 7

Reset to default 5

There are a couple of ways you can go about doing this.

You can configure your webserver to process files with extension .js with PHP and just inject your PHP there. Of course this means you need a way to actually calculate your variable there, and this would slow down serving your regular javascript content.

You can simply output the PHP variable to a Javascript variable within a <script> element like this

<script type="text/javascript">
var path = "<?php echo $path; ?>";
</script>

And then access this path variable in your AJAX. Most would probably use the second approach.

You can rename your thejsfile.js to thejsfile.php, add the following to the very beginning of it, and it should be parsed for PHP:

<?php header("Content-type: text/javascript"); ?>

Then reference it like this:

<head>
<script type="text/javascript" src="thejsfile.php"></script>
</head>

Your other option is to just set your server up to parse .js files for PHP.

In thephpfile.php,

<html>
<head>
<script type="text/javascript">
    var config = { url: '<?= $path; ?>' };
</script>
<script type="text/javascript" src="thejsfile.js"></script>
</head>

Then you can access it with config.url in your javascript.

You can put values in such way only into the files which are processed by PHP.
External JavaScript-files are not processed by PHP so they are linked as is without sustitution.

Unless you're making a post to another server via JSONP then you might just consider using a relative path hard-coded in your Javascript that way you don't need to send the path from the server to the client:

...
    $.ajax({
        type: "POST",
        url: "home.php",
        data: dataString,
        cache: false
    });
....

But, if this is a URL that changes frequently and it really does need to be configurable then you can echo out the PHP variable as script something like this"

<html>
...
<script src="thejsfile.js" type="text/javascript">
<script type="text/javascript">
    // 'path' is a variable that is defined in thejsfile.js
    path = '<?php echo htmlentities($path)  ?>';
</script>

...

Whenever you output info from the server to the client (especially as Javascript) you have to be REALLY careful that it is escaped to prevent scripting attacks (ie allowing people to inject javascript into your code). In this case there is no reason to allow any type of html characters.

When working with external JS files You could store the value in a hidden field and retrieve the fields value with JavaScript.

<input type="hidden" name="path" id="path" value="<?php echo $path; ?>" />

and in the javascript (i'll use jquery)

var path = $('#path').attr('value');

Obviously you could write the variable to the script tags on the page, but if you use any advanced type of patterns it may be hard to access that variable.

echo "<script type='text/javascript'>var AcodationInfoForm4 =".$path."</script>";

Just try to echo the way shown above and you can access your variable anywhere you want.As this will be available globally to all your js files on your page.

发布评论

评论列表(0)

  1. 暂无评论