PHP variable in Javascript (.js file)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WPPJames
    New Member
    • May 2010
    • 8

    PHP variable in Javascript (.js file)

    Please note that I have omitted some code below for readability.

    I have a .php file, feed.php:

    Code:
    <?php 
      require_once 'config.php';
      $a_url = $_POST["aurl"];
      $v_url = $_POST["vurl"];
    ?>
    
    <html>
    
    <head>
    
    <link rel="stylesheet" type="text/css" href="interstitial.css" />
    
    <script type="text/javascript" src="interstitial.js">
    
    </script>
    
    
    </head>
    
    <body>
    
    
    <iframe src = "<?=$v_url;?>" width="100%" height="100%" marginwidth="0" marginheight="0" frameborder="0" vspace="0" hspace="0" scrolling="no"></iframe>
    
    
    </body>
    </html>
    Please note the assigning of variable '$a_url' in the code above. Also note the loading of the interstitial.js file (<script type="text/javascript" src="interstiti al.js">).

    Here is my interstitial.js file:

    Code:
    var interstitialBox={
    displayfiles: ['<?php echo $a_url?>'],
    
    createcontainer:function(){
    //write out entire HTML for Interstitial Box:
    document.write('<div id="interContainer">'+this.defineheader+'<div id="interContent"></div></div><div id="interVeil"></div>')
    this.interContainer=document.getElementById("interContainer") //reference interstitial container
    this.interVeil=document.getElementById("interVeil") //reference veil
    this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
    },
    
    initialize:function(){
    this.createcontainer() //write out interstitial container
    this.ajaxconnect(this.displayfiles[Math.floor(Math.random()*this.displayfiles.length)], this.interContainer) //load page into content via ajax
    this.dotask(window, function(){interstitialBox.hidescrollbar(); interstitialBox.getscrollbarwidth(); setTimeout("interstitialBox.showcontainer()", 100)}, "load")
    this.dotask(window, function(){interstitialBox.showcontainer()}, "resize")
    }
    }
    I want to use the $a_url variable set in feed.php within the javascript code in the interstitial.js file. As you can see above, I tried using:

    displayfiles: ['<?php echo $a_url?>'],

    in addition to several other variations. Doesn't work. Any suggestions?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you try to access a PHP variable across different non-PHP files, which naturally doesn't work (PHP long finished working, when the browser loads the JavaScript file)

    Comment

    • usmanhalalit
      New Member
      • May 2010
      • 4

      #3
      Why don't you use just a single file?

      Try using interstitial.js code in feed.php inside <head> section:-
      Code:
      <script type="text/javascript">
      //here paste your interstitial.js code
      </script>
      [Removed link]

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Why don't you use just a single file?
        a) maintenance
        b) code re-use
        c) separation of content and behaviour

        Comment

        • usmanhalalit
          New Member
          • May 2010
          • 4

          #5
          Originally posted by Dormilich
          a) maintenance
          b) code re-use
          c) separation of content and behaviour
          OK let him try like this:-

          Change the JS file extension to PHP (interstitial.p hp), for example;

          interstitial.ph p:-
          Code:
          <?php
          header('Content-type: application/javascript'); 
          $myvar='usman';
          ?>
          //js starts
          alert('<?php echo $myvar ?>');
          feed.php:-

          Code:
          <html>
          <head>
          <script type='text/javascript' src='interstitial.php'></script>
          </head>
          <body>
          <?php
          	echo "hi";
          ?>
          </body>
          </html>

          Comment

          • WPPJames
            New Member
            • May 2010
            • 8

            #6
            usmanhalalit: You were right...the .js file did have to be renamed to .php. I think I have everything working now. You were a great help. Many thanks!

            Comment

            Working...