-1

I've read this post(how do i send a variable in the url in javascript), but can't get it to work. I must add that I don't know javascript at all. I'm trying to open an new "printer friendly" page in php, but in order for that I need to send the userid with the url, my code looks like this:

Page1

<?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()' />";

Page2(print.php)

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

echo $uid_print1;

But my ouput is: $userid

7 Answers 7

4
window.open("http://www.mysite.com/exams/print.php?uid_print=&lt;?php echo $userid; ?>",'','width=200,height=100')
Sign up to request clarification or add additional context in comments.

Comments

2

At the moment the variable in your javascript tag is not replaced because it is not enclosed by PHP tags. You can easily do it like this:

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

Comments

2

Modify your window.open line to looks like this:

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

You have to 'enter' PHP block, and print out $userid variable to HTML (JavaScript) output.

Comments

2

This line in your JS script:

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

Outputs '$userid' directly. You need to replace $userid with something like:

<?php echo $userid; ?>

Comments

2

Use <?php....?> tags:

...print.php?uid_print=<?php echo $userid;?>

Comments

2

Replacing:

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

With:

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

Should probably do the trick.

You can also use the shorthand <?= $userid ?> which, to my eyes is more readable.

Comments

2

please concatenate $userid in your javascript.

it can be concatenated like uid_print='.$userid.' and you will get the value in $_GET["uid_print"]

you can also echo the $userid in string like

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.