Passing a PHP variable in an external JS file

I read a lot of threads here, but I still can’t get the variable passed from PHP to an external JS file, and wondered if anyone could help?

In my PHP file, I have the following:

<script type="text/javascript"> var pass_this_variable = <?php $company['website']; ?>; </script> <script type="text/javascript" src="/js/track.js"></script> 

In the JS file, I have the following:

 document.write('<IFRAME SRC="$company['website']" WIDTH="300" HEIGHT="400"></IFRAME>'); 

What I'm trying to achieve is to open an IFRAME and populate what is contained in $ company ['website']. I know that I can just use the IFRAME directly in the PHP file, but that’s not what I was assigned to do my homework. When I use the IFRAME directly in the PHP file, it works fine, and if I provide a static URL in the JS file, for example http://www.google.com , it also works fine.

Can anyone help? Thanks


EDIT:

Thanks for the answers so far, however I still can't get it to work :(

The frame that I have in track.php (or track.js) will not load the URL specified in $company['website'] , but if I change it to http://www.google.com it working tone. For some reason, the value of $company['website'] not passed: (

+4
source share
6 answers

if you want your external javascript to be dynamic, you can make it a php file and give the correct header, for example:

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

track.php

 <?php // javascript generator Header("content-type: application/x-javascript"); ?> document.write('<IFRAME SRC="<?php echo $company['website'] ?>" WIDTH="300" HEIGHT="400"></IFRAME>'); 
+8
source

PHP file (don't forget the echo and quoting):

 <script type="text/javascript"> var pass_this_variable = '<?php echo $company['website']; ?>'; </script> <script type="text/javascript" src="/js/track.js"></script> 

JS file (use pass_this_variable instead):

 document.write('<IFRAME SRC="'+pass_this_variable+'" WIDTH="300" HEIGHT="400"></IFRAME>'); 
+6
source

You should fix this line: var pass_this_variable = <?php echo $company['website']; ?>; var pass_this_variable = <?php echo $company['website']; ?>;

Adding echo and it should work

+2
source

Call a PHP file inside a JavaScript source. You can find the tutorial here:

http://www.javascriptkit.com/javatutors/externalphp.shtml .

So your code will be like this:

 <script type="text/javascript" src="track.php?company=<?php echo $company['website']; ?>"></script> 

In a PHP file, you can get the value through the $_GET variable and use it in an iframe . Make sure you sanitize the entrance.

+2
source

JavaScript provides you with ajax functionality for reading PHP or text files. Why don't you create an HTML iframe inside the PHP file with your variables, and then return the response and throw it inside the div.

+2
source

Code for your PHP file:

 $cmp = $company['website']; echo '<input type="hidden" id="cmp1" name="cmp1" value="' . $cmp . '" />'; 

Code for your JavaScript file (.js) to get the value of a PHP file:

 var company = document.getElementById('cmp').value; 
+2
source

Source: https://habr.com/ru/post/910677/


All Articles