send php variable value via javascript in url - php

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

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

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')

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.

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

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

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.

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

Related

Pass $_SESSION to next statement

I developing a Intranet and I'm a bit stuck with allowing access for individual users. All works fine when I limit the access to an element, if that element in at the bottom/last element. I need this to usable where ever I want. If you are in the Directors group, get the element. If you are in the All group only, you get nothing. Any help would be great.
The HTML:
.....
<?php include('admin/Directors.php');
echo 'foooooo':
?>
....
<?php include('admin/All.php');
echo 'baaaar':
?>
...
The PHP (Directors.php):
<?php
session_start();
$allowed_users = array('mark','joe','allan');
if(!in_array($_SESSION['user'],$allowed_users)) die('');
?>
From wat I understand is happening here is that its reading the Directors.php file and applying it to entire HTML file.
Try this In your Directors.php:
session_start();
$allowed_users = array('mark','joe','allan');
return in_array($_SESSION['user'],$allowed_users));
And this in your html:
$allowed = include('admin/Directors.php');
if($allowed)
{
echo 'foooooo';
}
Instead of killing script with die() simply return the evaluation value check it in your html. But if there is other stuff in Director.php you can do this.
Try this In your Directors.php:
session_start();
$allowed_users = array('mark','joe','allan');
$allowed =in_array($_SESSION['user'],$allowed_users));
And this in your html:
include('admin/Directors.php');
if($allowed)
{
echo 'foooooo';
}

Pass PHP variable with GET to another PHP page [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
There's probably something pretty easy here that I'm missing but I can't seem to figure it out.
I'm trying to pass a PHP variable "$session" from one PHP page to another PHP page using GET. The GET shows in the new URL but the value is not passed to the page so that I can retrieve it. Below is the relevant code.
On the main Index.php page.
<div class="mapgroup">
<table>
<tr>
<td></td>
<td id="refreshKML" ></td>
<?php echo "<td><a href='3D-Earth.php?session=$session' title='Full Size Google Earth' target='_blank' >Click here</a> for full size Google Earth</td>"; ?>
</tr>
</table>
<div id="map3d"></div>
</div>
The "$session" variable is passed to 3D-Earth.php using GET and the correct value shows in the URL. However, when I try to retrieve the variable with:
<?php
$session = $_GET["session"];
?>
and then try to create a javascript variable:
<script>
var session = <?php echo $session; ?>;
</script>
that I will use to concatenate the below string:
var href = 'http://localhost/satellites/' + session + '/master.kml';
nothing is being passed.
I appreciate any help anyone can provide. Thanks in advance.
Additionally, I've inserted the below code at the bottom of my 3D-Earth.php page to verify that the GET is being passed.
<div>
<table><p style='color:white;' >Hello " <?php echo $session; ?> " world!</p></table>
</div>
The result is an empty string that shows: Hello " " World! Anyone know what I might be doing wrong?
Try like this:
var session = '<?php echo $session; ?>';
I suggest stop using names like 'session' that might conflict with php reserved words. Maybe that's not the issue here, but avoids many other problems and confusions.
Also in javascript put the echo in quotes:
var session = '<?php echo $session; ?>';
You should put a single quotes around your variable like this
var session = '<?php echo $session; ?>';
As above answers say, you have to have single quotes around a php statement when you want to use it in javascript. So var session = 'php statement'; is the right way to go.
have you tried to put this code
<script>
var session = <?php echo $session; ?>;
</script>
under this code ??
<?php
$session = $_GET["session"];
?>

show hide div based on mysql table row value

hi guys im trying to show and hide div according to mysql value but i couldnt do it can you help me what im doing wrong
here is my code thanks a lot for your ideas
var Value = <?php echo json_encode($valuek) ?>;
if (Value==1){
$('#show_hide').show();
}
else{
$('#show_hide').hide();
}
<?php
$valuek = $session->userinfo['vcc'];
?>
<div id="show_hide">
some code
</div>
<?php echo json_encode($valuek) ?>
will return a json string, instead try just using "echo"
<?php echo $valuek ?>
If all you are going for is a boolean value then there is simply no need for JSON.
Echo the value directly into the JavaScript. Remember to ensure you are passing a valid boolean value.
PHP code -
<?php
$showDiv = ($dbValue == 1? 'true' : 'false');
?>
JavaScript + PHP injection -
<script>
var value = '<?php echo $showDiv; ?>';
<script>
Don't forget to wrap the PHP injected value with quotes.
$valuek = $session->userinfo['vcc'];
I'm not sure if you have the code in this order in your php file, or just showed pieces of code in this order, but Should go BEFORE your js code. It has no value when js code is run.
To see what $valuek is, just echo it on top of the screen
<?php echo "<h1>$valuek</h1>" ?>.
Or just look at the source - at your js function, to see what is printed after 'var Value ='
That's the main thing really - to make sure that you getting what you expect from session.
And as been said, you don't need jason_encode, but you do need a semi-colon after echo command.
Also, I hope your jquery code is within $(document).ready function, not as is.

How to get javascript post variable in php?

I have a small problem to get jQuery post variables. So i have javascript file where is code bellow:
<script type="text/javascript">
$.post("view.php", { name: "John" } );
</script>
And i try to get that name "John" in view.php file like this:
<?php $variable = $_GET["name"]; ?>
And it wont get that name. Can someone please help me?
The variable values are not included in $_GET you might find them in $_POST
<?php $variable = $_POST["name"]; ?>
Since you've used '$.post' you must use the related action which will be $_POST. If you were using '$.get', then you would use your current method of $_GET.
Use $_POST instead of $_GET...
And you can use
<?php $variable = $_REQUEST['name']; ?>
if not shure how it should be get.

passing variables from php file to anther

How to pass variables from a php file to another while it is not html inputs ,just i have a link refer to the other file and i want to pass variables or values to it
Example:
File1.php
<?php
$name='OdO';
echo "<a href='File2.php'>Go To File2</a>";
?>
File2.php
<?php
echo $name;
?>
Use sessions to store any small value that needs to persist over several requests.
File1.php:
session_start();
$_SESSION['var'] = 'foo';
File2.php:
session_start();
$var = $_SESSION['var']; // $var becomes 'foo'
Try to use sessions. Or you can send a GET parameters.
You can use URLs to pass the value too.
like
index.php?id=1&value=certain
and access it later like
$id = $_GET['id'];
$value = $_GET['value'];
However, POST might be much reliable. Sessions/Cookies and database might be used to make the values globally available.
Here's one (bad) solution, using output buffering:
File 1:
<?php
$name = 'OdO';
echo 'Go To File2';
?>
File 2:
<?php
ob_start();
include 'File1.php';
ob_end_clean();
echo $name;
?>

Categories