Page 1 of 1

Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 1:20 am
by alex4orly
At the top of the page, Before the html tag, I have this

<?php
session_start();
$str = "f=6&t=3&e=1&view=unread#unread";
$_SESSION['params'] = $str;
?>


Inside the HTML in the head section I have this:

<script>
window.onload = function()
{
var url = "http://beleuramyhome.org.au/phpBB3/viewtopic.php?";
var args = "<?php echo $_SESSION['params'] ?>";
url += args;

alert(url);
}
</script>


But what I get is this , Why?

http://beleuramyhome.org.au/phpBB3/viewtopic.php?<?php echo $_SESSION['params'] ?>

Instead of the combined strings?

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 6:08 am
by Pablo
Is the other PHP code parsed correctly?

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 6:23 am
by alex4orly
Yes, plenty, but not in this page.
This seems to me such a simple thing to do...

Why not working?

Thanks

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 6:43 am
by Pablo
The code looks correct as far as I can tell.

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 7:38 am
by WWBman

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 8:34 am
by alex4orly
No, the link to get here on the website in my email is without that extra slash...

"viewtopic.php?f=75&t=81069&e=1&view=unread#unread"

I am lost with this...

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 9:27 am
by WWBman
I have just tried your code (without the /) and it seems to work!
Image

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 9:35 am
by alex4orly
Yes i know, when i use it all as one string it works.
It also works if i add the two strings as strings
It doesn't work if i try to get the arguments using the $_SESSION...

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 9:42 am
by WWBman
I have replicated exactly your code:

Code: Select all

<?php
session_start();
$str = "f=6&t=3&e=1&view=unread#unread";
$_SESSION['params'] = $str;
?><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
<link href="index.css" rel="stylesheet">
<script>   
   window.onload = function()
   {
   var url = "http://beleuramyhome.org.au/phpBB3/viewtopic.php?"; 
   var args = "<?php echo $_SESSION['params'] ?>";
   url += args; 
   
   alert(url);
   }
</script>
</head>
<body>
   <div id="container">
   </div>
</body>
</html>

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 8:36 pm
by alex4orly
Well, it seems something is wrong at my end.
In design mode, I insert the Javascript script into the head section,
but when looking at the uploaded result on the server, it is missing the entire script
This will explain why, what seems to be a simple thing, doesn't work.
Below is the source of the page

<?php
session_start();
$str = "f=6&t=3&e=1&view=unread#unread";
$_SESSION['params'] = $str;
?><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
<meta name="generator" content="WYSIWYG Web Builder 14 - http://www.wysiwygwebbuilder.com">
<style>
body
{
background-color: transparent;
color: #000000;
font-family: Arial;
font-weight: normal;
font-size: 13px;
line-height: 1.1875;
margin: 0;
padding: 0;
}
</style>
<link href="MasterFrame.css" rel="stylesheet">
</head>
<body>
<script src="iframeurl.js"></script>
</body>
</html>

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 8:41 pm
by Pablo
Maybe you have enabled "Generate external file for page specific JavaScripts" in Tools->Options->HTML?
This will move the script to another file and therefore it will not be processed by the PHP parser,.

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 9:02 pm
by alex4orly
Yes, the two options were checked, have no idea how.
Anyway, the generated code below is still not doing it, why?

<?php
session_start();
$str = "f=6&t=3&e=1&view=unread#unread";
$_SESSION['params'] = $str;
?><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
<meta name="generator" content="WYSIWYG Web Builder 14 - http://www.wysiwygwebbuilder.com">
<style>
body
{
background-color: transparent;
color: #000000;
font-family: Arial;
font-weight: normal;
font-size: 13px;
line-height: 1.1875;
margin: 0;
padding: 0;
}
</style>
<link href="MasterFrame.css" rel="stylesheet">
<script>
window.onload = function()
{
var url = "http://beleuramyhome.org.au/phpBB3/viewtopic.php?";
var args = <?php echo $_SESSION['params'] ?>;
url += args;

//document.getElementById("InlineFrame1").setAttribute("src", url);

alert(url);
}
</script>
</head>
<body>
</body>
</html>

Re: Using SESSION variable in a Javascript

Posted: Thu May 31, 2018 11:07 pm
by alex4orly
I mean, not showing the alert()...

Re: Using SESSION variable in a Javascript

Posted: Fri Jun 01, 2018 6:09 am
by Pablo
Did you look in the browser's debugger for errors? (F12)

Re: Using SESSION variable in a Javascript

Posted: Fri Jun 01, 2018 6:24 am
by alex4orly
Hi Pablo,
Just fixed it, there was a mismatch generated with the CSS
I created again the page from scartch and all good

Thanks again