1

I have read the following posts, but they are a little over the top for me. I think I'm trying to do something fairly simple, and would like some guidance.

How to pass variables and data from PHP to JavaScript?

passing PHP variables across javascript windows.open to another PHP page

Posting a php variable to a new window

Here is the case:

I have a php script which is very simple, it calls another script and passes 2 variables:

<?php
echo '<script type="text/javascript" language="javascript">
window.open("http://callpage.com/utils/cdr.php?callernum=123456789&calltime=2017-02-22 16:24:12");
</script>';
?>

Note: This is just a "hardcoded" example.

The next script, takes those numbers and builds file/url variable.

Lets say

$file = /var/www/html/file.wav

What I'm trying to do open a new window to the effect of :

http://newpage.com/$file

I have read and found that I think the best use is Javascript, but I can't seem to get my variable into the Javascript.

Here is what I would like to get working:

<?php 
$file = /var/www/html/file.wav
echo '<script type="text/javascript" language="javascript"> 
window.open("http://newpage.com/$file"); 
</script>'; 
?>

A few notes:

  • I don't want to "redirect" the old page, I want it to stay open, and the remote page isn't on the same domain (one is a.domain.com and the other is b.domain.com).
  • I don't care about window sizes, etc, its a wav file that I'm expecting the browser to just play with a simple Browser default interface for Wav.
FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57

3 Answers3

2

if I understood correctly what you want, you have to concatenate the string with the variable in order to be replaceed

<?php
$file = '/var/www/html/file.wav';
echo '<script type="text/javascript" language="javascript"> 
          window.open("http://newpage.com/'.$file.'");
      </script>';
?>
nikos.svnk
  • 1,375
  • 1
  • 13
  • 24
1

Use string interpolation with double quotes for the echo statement and single quotes everywhere inside the javascript:

echo "<script type='text/javascript' language='javascript'> 
window.open('http://newpage.com/$file'); 
</script>";

The interpolated PHP variable $file should be correctly interpreted as a string and the value it holds should be displayed in the URI of your javascript. Check out this easy to understand info about variable interpolation http://phppot.com/php/variable-interpolation-in-php/

atomCode
  • 842
  • 7
  • 17
  • I prefer this method, as the double quotes take care of all variables added, vs the other answer seems prone to mistakes, but both worked! so +1 for both. – FreeSoftwareServers Feb 28 '17 at 02:10
  • I am at risk of not being able to ask any more questions, if you think this was well documented and although a duplicate, worth "re-asking", consider an upvote. – FreeSoftwareServers Feb 28 '17 at 02:47
0

Here is my "final" code snippet:

$query->execute();

while ($row = $query->fetch(PDO::FETCH_ASSOC))
 {
  $uid = $row['uniqueid'];

  foreach(glob($path. "*". $uid. "*") as $file) {
  $link = "http://newpage.com$file";

  echo "<script type='text/javascript' language='javascript'>
  window.open('$link');
  </script>";

  }
 }
FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57