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"];
?>
Related
I'm trying to retrieve the "custom Phone" value from a form where the variables are being passed through a URL and then retrieved using the PHP Get method.
Here is the URL with the pass through variables:
http://domain.com/redirect.php?custom%20phone=555-555-5555&email=jk%40blackdiamondacademy%2ecom&from=jk%40blackdiamondacademy%2ecom&listname=fgxpressusa&meta_adtracking=&meta_message=1&meta_required=name%2cemail%2ccustom%20Phone&name=Jordan&submit=Submit&submit.x=121&submit.y=27
Here is the code on my landing page that has the Get PHP code:
<script type="text/javascript">
var sale = PostAffTracker.createAction('lead');
sale.setOrderID('<?php echo $_GET['meta_message']; ?>');
sale.setProductID('<?php echo $_GET['unit']; ?>');
sale.setData1('<?php echo $_GET['name']; ?>');
sale.setData2('<?php echo $_GET['email']; ?>');
sale.setData3('<?php echo $_GET['custom%20phone']; ?>');
PostAffTracker.register();
</script>
The code retrieves the "name", and "email" but not the "custom%20phone". I tried putting 'custom phone' as the get value as well as 'custom+phone' and still it doesn't retrieve it.
The issue seems to be the space in the value from my web form ("custom Phone") that is being passed through the URL as "custom%20phone".
Any help would be greatly appreciated!
I just found the answer to my own question through trial and error.
The PHP GET method translates %20 as an underscore so I put:
<?php echo $_GET['custom_phone']; ?>
Works perfect!
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the best way to pass a PHP variable to Javascript?
I am using the following code:
<script type="text/javascript">
<?php $ctnme = $_SERVER['REQUEST_URI'];
$cnme = explode("/",$ctnme);
echo $cname = $cnme[1];
?>
var spge = <?php echo $cname; ?> ;
alert(spge);
</script>
The value doesn't alert. What is the mistake?
Essentially:
<?php
//somewhere set a value
$var = "a value";
?>
<script>
// then echo it into the js/html stream
// and assign to a js variable
spge = '<?php echo $var ;?>';
// then
alert(spge);
</script>
The most secure way (in terms of special character and data type handling) is using json_encode():
var spge = <?php echo json_encode($cname); ?>;
Use json_encode() if possible (PHP 5.2+).
See this one (maybe duplicate?): Pass a PHP string to a JavaScript variable (and escape newlines)
Put quotes around the <?php echo $cname; ?> to make sure Javascript accepts it as a string, also consider escaping.
**var spge = '';**
alert(spge);
First timer to the site, not an overly experienced php programmer here :)
I have a problem, i am using an iframe within a site which im attempting to use a session variable inside, firstly ive just been trying to display the session variable to make sure they are accessible from within the iframe:
echo "session of productcheck ".$_SESSION['productcheck']." ";
echo "session of productcheck1 ".$_SESSION['productcheck1']." ";
echo "session of productcheck2 ".$_SESSION['productcheck2']." ";
echo "session of productcheck3 ".$_SESSION['productcheck3']." ";
this just shows "session of product check" with nothing after each one, I set the session variable as such:
$_SESSION['productcheck'] = $productBox;
the $productBox is a GET from the URL:
echo " <iframe src=\"homeview.php?productBox=$product1\" name=\"FRAMENAME\" width=\"594\" height=\"450\" scrolling=\"No\" id=\"FRAMENAME\" allowautotransparency=\"true\" > </iframe >";
What's strange is if i just take the $productBox variable retrieved from the URL and use that then the code works, its only when i store it in a session variable that it gets confused. I want to retrieve a second $productBox and assign it to session var productcheck1 and so forth down the line. Unfortunately, i have to take one var in at a time, otherwise i could just pass all 4 products and not worry about the sessions.
Perhaps I'm making this far too complicated, any help would be much appreciated Thank you!!
You have to use session_start() in both scripts, the one setting the values (and presumably printing the <iframe>-element?) and the script that produces the contents for the iframe.
e.g. the "outer" script
<?php // test.php
session_start();
$_SESSION['productcheck'] = array();
$_SESSION['productcheck'][] = 'A';
$_SESSION['productcheck'][] = 'B';
$_SESSION['productcheck'][] = 'C';
session_write_close(); // optional
?>
<html>
<head><title>session test</title></head>
<body>
<div>session test</div>
<iframe src="test2.php" />
</body>
</html>
and the script for the iframe content
<?php // test2.php
session_start();
?>
<html>
<head><title>iframe session test</title></head>
<body>
<div>
<?php
if ( isset($_SESSION['productcheck']) && is_array($_SESSION['productcheck']) ) {
foreach( $_SESSION['productcheck'] as $pc ) {
echo $pc, "<br />\n";
}
}
?>
</div>
</body>
</html>
Not sure what's up with your session variables, but you can definitely pass all four variables through the url in your iframe. You just need to separate your key value pairs with an ampersand. So something like this:
file.php?key1=val1&key2=val2&key3=val3 and so on.
This is probably a better way than using session variables if your just trying to get data into that other file.
How may I retrieve, by JS (and I mean an external script), the value of some variables assigned in a php include file like the one below?
<?php
$var1 = "a";
$var2 = "foo";
?>
Assuming that you mean using an AJAX request to retrieve the variables...the best way to do that would be:
<?php
$array["var1"]="a";
$array["var2"]="foo";
echo json_encode($array);
?>
And on the JS end, you would want to do:
json = eval( "(" + response + ")" );
And var1 and var2 would be json.var1/json.var2
Edit:
In that case you should be able to do something like:
<script type="text/javascript">
var phpvars = <?php echo json_encode($array); ?>;
<script>
And just place that above where whistle.js will be included, and then the Javascript in that file will be able to access the variables through phpvars. (Changing the variables.php file so that it has the same format as above, except no echoing of it).
To reiterate the previous feedback, PHP is used to generate HTML -- The PHP file itself is never available to the browser. You can use the variables.php to generate hidden tags, then JavaScript to read them.
eg,
variables.php output:
<div id='varA' style='display:none'>foo</div>
javascript:
document.getElementById('varA').innerText
or
variables.php output:
<input id='varB' type='hidden' value='bar' />
javascript:
document.getElementById('varB').value