473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

PHP variable in Javascript (.js file)

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

I have a .php file, feed.php:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.   require_once 'config.php';
  3.   $a_url = $_POST["aurl"];
  4.   $v_url = $_POST["vurl"];
  5. ?>
  6.  
  7. <html>
  8.  
  9. <head>
  10.  
  11. <link rel="stylesheet" type="text/css" href="interstitial.css" />
  12.  
  13. <script type="text/javascript" src="interstitial.js">
  14.  
  15. </script>
  16.  
  17.  
  18. </head>
  19.  
  20. <body>
  21.  
  22.  
  23. <iframe src = "<?=$v_url;?>" width="100%" height="100%" marginwidth="0" marginheight="0" frameborder="0" vspace="0" hspace="0" scrolling="no"></iframe>
  24.  
  25.  
  26. </body>
  27. </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="interstitial.js">).

Here is my interstitial.js file:

Expand|Select|Wrap|Line Numbers
  1. var interstitialBox={
  2. displayfiles: ['<?php echo $a_url?>'],
  3.  
  4. createcontainer:function(){
  5. //write out entire HTML for Interstitial Box:
  6. document.write('<div id="interContainer">'+this.defineheader+'<div id="interContent"></div></div><div id="interVeil"></div>')
  7. this.interContainer=document.getElementById("interContainer") //reference interstitial container
  8. this.interVeil=document.getElementById("interVeil") //reference veil
  9. this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
  10. },
  11.  
  12. initialize:function(){
  13. this.createcontainer() //write out interstitial container
  14. this.ajaxconnect(this.displayfiles[Math.floor(Math.random()*this.displayfiles.length)], this.interContainer) //load page into content via ajax
  15. this.dotask(window, function(){interstitialBox.hidescrollbar(); interstitialBox.getscrollbarwidth(); setTimeout("interstitialBox.showcontainer()", 100)}, "load")
  16. this.dotask(window, function(){interstitialBox.showcontainer()}, "resize")
  17. }
  18. }
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?
May 15 '10 #1

✓ answered by usmanhalalit

@Dormilich
OK let him try like this:-

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

interstitial.php:-
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header('Content-type: application/javascript'); 
  3. $myvar='usman';
  4. ?>
  5. //js starts
  6. alert('<?php echo $myvar ?>');
feed.php:-

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type='text/javascript' src='interstitial.php'></script>
  4. </head>
  5. <body>
  6. <?php
  7.     echo "hi";
  8. ?>
  9. </body>
  10. </html>

5 2689
Dormilich
8,658 Expert Mod 8TB
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)
May 15 '10 #2
Why don't you use just a single file?

Try using interstitial.js code in feed.php inside <head> section:-
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. //here paste your interstitial.js code
  3. </script>
[Removed link]
May 15 '10 #3
Dormilich
8,658 Expert Mod 8TB
Why don't you use just a single file?
a) maintenance
b) code re-use
c) separation of content and behaviour
May 15 '10 #4
@Dormilich
OK let him try like this:-

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

interstitial.php:-
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header('Content-type: application/javascript'); 
  3. $myvar='usman';
  4. ?>
  5. //js starts
  6. alert('<?php echo $myvar ?>');
feed.php:-

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type='text/javascript' src='interstitial.php'></script>
  4. </head>
  5. <body>
  6. <?php
  7.     echo "hi";
  8. ?>
  9. </body>
  10. </html>
May 16 '10 #5
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!
May 16 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stefano | last post by:
Posted: Fri Jan 23, 2004 3:59 pm Post subject: Variable Javascript -------------------------------------------------------------------------------- Hi all, i have a problem with...
3
by: Antonio | last post by:
Good Morning, I would want to insert the javascript of google adsense in my site, here it is : <script type="text/javascript"><!-- google_ad_client = "pub-4902392511135327"; google_ad_width =...
21
by: ryanmhuc | last post by:
I know the subject might be confusing. I am no beginner with javascript but I haven't been able to figure out how to get the javascript file name from code inside the file. So you have an HTML...
3
by: Angel | last post by:
Is there any way to debug a javascript file that I included in my ASP.net project. or some reason when I call a javascript function in the file from my ASP.NET code I get an error message. now if i...
24
by: gizoto316 | last post by:
I was wondering if it was possible to have php code in a javascript file. I have a java file with a bunch of functions on it and I wanted to add one where the action has php code in it. When i...
1
by: anjud | last post by:
I have an external javascript file which contains a global variable - arrNames var arrNames=new Array(); arrNames="FirstName"; arrNames="LastName"; In my html page I had given the path of...
17
by: NeoAlchemy | last post by:
I am starting to find more web pages that are using a query parameters after the JavaScript file. Example can be found at www.opensourcefood.com. Within the source you'll see: <script...
9
by: Mahernoz | last post by:
Hello Friends, The JavaScript File exmplmenu_var.js contains the code... (for the sake of brevity i am showing only that code which needs to be changed) I am actually developing a menu using...
1
by: runway27 | last post by:
i have a register.php and confirmregister.php file. in the first file the user fills a registeration form and in the second page they can review what they entered in the first page and then...
29
by: FreshRob | last post by:
I have been trying to fix this issue the whole of today and have gotten no where. I am developing a new website, and wanted it to display a webpage in lightbox and have an external page added to the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.