2012-03-06 109 views
-1

我读过this posthow do i send a variable in the url in javascript),但无法使其正常工作。我必须补充说,我根本不知道JavaScript。我想在PHP中打开一个新的“打印机友好”的页面,但为了我需要的URL发送用户ID,我的代码看起来是这样的:通过url在url中发送php变量值

第1页

<?php 
session_start(); 
$userid = $_SESSION['userid']; 
?> 
<head> 
<script type="text/javascript"> 
function open_win() 
{ 
window.open("http://www.mysite.com/exams/print.php?uid_print=$userid",'','width=200,height=100') 
} 
</script> 
</head> 
<?php 
if ($_SESSION['auth']) { 
include 'datalogin.php'; 

echo "<input type='button' value='Print this page' onclick='open_win()' />"; 

第二页(print.php)

<?php 
session_start(); 
include 'datalogin.php'; 
$uid_print1 = $_GET['uid_print']; 

echo $uid_print1; 

但我的输出继电器是:$用户ID

回答

2

目前在JavaScript代码的变量没有被替换,因为它不是由封闭PHP标签。你可以很容易地这样做:

window.open("http://www.mysite.com/exams/print.php?uid_print=<?php echo $userid; ?>",'', 'width=200,height=100') 
2

修改您的window.open线看起来像这样:

window.open("http://www.mysite.com/exams/print.php?uid_print=<?php echo $userid; ?>",'','width=200,height=100'); 

你必须 '进入' PHP块,并打印出$userid变量HTML(JavaScript)的输出。

2

这条线在你的JS脚本:

window.open("http://www.mysite.com/exams/print.php?uid_print=$userid",'','width=200,height=100') 

输出 '$用户ID' 直接。你需要的东西,如更换$用户名:

<?php echo $userid; ?> 
2

使用<?php....?>标签:

...print.php?uid_print=<?php echo $userid;?> 
4
window.open("http://www.mysite.com/exams/print.php?uid_print=&lt;?php echo $userid; ?>",'','width=200,height=100') 
2

更换:

window.open("http://www.mysite.com/exams/print.php?uid_print=$userid",'','width=200,height=100') 

有了:

window.open("http://www.mysite.com/exams/print.php?uid_print=<?php echo $userid; ?>",'','width=200,height=100') 

应该可以做的。

你也可以使用其中的shorthand<?= $userid ?>,这对我来说更具可读性。

2

请在您的javascript中连接$ userid。

它可以像uid_print ='。$ userid。'一样连接在一起。您将获得价值$ _GET [“uid_print”]

你也可以呼应字符串$用户ID喜欢