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: 532
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Using SESSION variable in a Javascript

Post by alex4orly » Thu May 31, 2018 1:20 am

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: 13084
Joined: Tue Mar 28, 2006 12:00 pm
Location: Europe
Contact:

Re: Using SESSION variable in a Javascript

Post by Pablo » Thu May 31, 2018 6:08 am

Is the other PHP code parsed correctly?

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

Re: Using SESSION variable in a Javascript

Post by alex4orly » Thu May 31, 2018 6:23 am

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: 13084
Joined: Tue Mar 28, 2006 12:00 pm
Location: Europe
Contact:

Re: Using SESSION variable in a Javascript

Post by Pablo » Thu May 31, 2018 6:43 am

The code looks correct as far as I can tell.

WWBman
 
 
Posts: 576
Joined: Fri Jan 08, 2010 6:10 pm

Re: Using SESSION variable in a Javascript

Post by WWBman » Thu May 31, 2018 7:38 am


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

Re: Using SESSION variable in a Javascript

Post by alex4orly » Thu May 31, 2018 8:34 am

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: 576
Joined: Fri Jan 08, 2010 6:10 pm

Re: Using SESSION variable in a Javascript

Post by WWBman » Thu May 31, 2018 9:27 am

I have just tried your code (without the /) and it seems to work!
Image

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

Re: Using SESSION variable in a Javascript

Post by alex4orly » Thu May 31, 2018 9:35 am

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: 576
Joined: Fri Jan 08, 2010 6:10 pm

Re: Using SESSION variable in a Javascript

Post by WWBman » Thu May 31, 2018 9:42 am

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: 532
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Using SESSION variable in a Javascript

Post by alex4orly » Thu May 31, 2018 8:36 pm

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: 13084
Joined: Tue Mar 28, 2006 12:00 pm
Location: Europe
Contact:

Re: Using SESSION variable in a Javascript

Post by Pablo » Thu May 31, 2018 8:41 pm

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: 532
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Using SESSION variable in a Javascript

Post by alex4orly » Thu May 31, 2018 9:02 pm

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: 532
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Using SESSION variable in a Javascript

Post by alex4orly » Thu May 31, 2018 11:07 pm

I mean, not showing the alert()...

User avatar
Pablo
 
Posts: 13084
Joined: Tue Mar 28, 2006 12:00 pm
Location: Europe
Contact:

Re: Using SESSION variable in a Javascript

Post by Pablo » Fri Jun 01, 2018 6:09 am

Did you look in the browser's debugger for errors? (F12)

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

Re: Using SESSION variable in a Javascript

Post by alex4orly » Fri Jun 01, 2018 6:24 am

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

Who is online

Users browsing this forum: No registered users and 3 guests