Using SESSION variable in a Javascript

This section is for posting questions which are not directly related to WYSIWYG Web Builder.
Examples of off topics: web server configuration, hosting, programming related questions, third party scripts.

Note that these questions will generally not be answered by the administrators of this forum.
Post Reply
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Using SESSION variable in a Javascript

Post 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?
User avatar
Pablo
 
Posts: 21575
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Using SESSION variable in a Javascript

Post by Pablo »

Is the other PHP code parsed correctly?
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Using SESSION variable in a Javascript

Post by alex4orly »

Yes, plenty, but not in this page.
This seems to me such a simple thing to do...

Why not working?

Thanks
User avatar
Pablo
 
Posts: 21575
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Using SESSION variable in a Javascript

Post by Pablo »

The code looks correct as far as I can tell.
WWBman
 
 
Posts: 916
Joined: Fri Jan 08, 2010 6:10 pm

Re: Using SESSION variable in a Javascript

Post by WWBman »

User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Using SESSION variable in a Javascript

Post 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...
WWBman
 
 
Posts: 916
Joined: Fri Jan 08, 2010 6:10 pm

Re: Using SESSION variable in a Javascript

Post by WWBman »

I have just tried your code (without the /) and it seems to work!
Image
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Using SESSION variable in a Javascript

Post 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...
WWBman
 
 
Posts: 916
Joined: Fri Jan 08, 2010 6:10 pm

Re: Using SESSION variable in a Javascript

Post 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>
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Using SESSION variable in a Javascript

Post 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>
User avatar
Pablo
 
Posts: 21575
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Using SESSION variable in a Javascript

Post 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,.
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Using SESSION variable in a Javascript

Post 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>
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Using SESSION variable in a Javascript

Post by alex4orly »

I mean, not showing the alert()...
User avatar
Pablo
 
Posts: 21575
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Using SESSION variable in a Javascript

Post by Pablo »

Did you look in the browser's debugger for errors? (F12)
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Using SESSION variable in a Javascript

Post 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
Post Reply