简体   繁体   English

PHP传递变量到新的Window.Open Javascript

[英]PHP Pass Variable to new Window.Open Javascript

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? 如何将变量和数据从PHP传递给JavaScript?

passing PHP variables across javascript windows.open to another PHP page 将PHP变量传递到javascript windows.open到另一个PHP页面

Posting a php variable to a new window 将php变量发布到新窗口

Here is the case: 情况如下:

I have a php script which is very simple, it calls another script and passes 2 variables: 我有一个非常简单的PHP脚本,它调用另一个脚本并传递2个变量:

<?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. 下一个脚本,获取这些数字并构建file / url变量。

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. 我已经阅读并发现我认为最好用的是Javascript,但我似乎无法将我的变量放入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). 我不想“重定向”旧页面,我希望它保持打开状态,并且远程页面不在同一个域中(一个是a.domain.com,另一个是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. 我不关心窗口大小等,它是一个wav文件,我希望浏览器只使用Wav的简单浏览器默认界面。

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>';
?>

Use string interpolation with double quotes for the echo statement and single quotes everywhere inside the javascript: 在javascript中使用带有双引号的字符串插值和echo语句中的单引号:

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. 内插的PHP变量$ file应该被正确解释为一个字符串,它所拥有的值应该显示在你的javascript的URI中。 Check out this easy to understand info about variable interpolation http://phppot.com/php/variable-interpolation-in-php/ 查看关于变量插值的这个易于理解的信息http://phppot.com/php/variable-interpolation-in-php/

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>";

  }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM