2012-03-06 111 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喜歡