CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
[SOLVED]How to pass variable value from perl to Javascript

 



terrykhatri531
User

Jul 30, 2014, 7:41 PM

Post #1 of 38 (39290 views)
[SOLVED]How to pass variable value from perl to Javascript Can't Post

Hi,

Does anyone know how to pass a variable value to javascript, here is the related portion of javascript :


Code
  
// <![CDATA[
document.writeln('<div class="dc-rnav"><a href="javascript:history.go(-1)" title="Go Back">Go Back</a> | <a href="login.pl" title="This will log you out">Log out</a> | <a href="" title="" >$user</a></div>');

...........


Here is how I pass the variable i.e. $user value to my perl script which also uses the above javascript:


Code


It just shows up as $user on the web page - see the picture attached.

Many many thanks !!

Terry


(This post was edited by terrykhatri531 on Aug 14, 2014, 2:13 PM)
Attachments: UserVariable.png (4.77 KB)


FishMonger
Veteran / Moderator

Jul 31, 2014, 6:27 AM

Post #2 of 38 (39282 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

You used the wrong quotes. Wrapping the entire string in single quotes prevents var interpolation.

See the "Quote and Quote-like Operators" section of perldoc perlop
http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators


terrykhatri531
User

Jul 31, 2014, 7:47 AM

Post #3 of 38 (39277 views)
Re: [FishMonger] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi Ron,

That's the Java script that's how it came meaning I found it on the internet and all its functionality works fine, if I try to change the single quotes the script does not work.

Rgds.

Terry


FishMonger
Veteran / Moderator

Jul 31, 2014, 8:40 AM

Post #4 of 38 (39271 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

Javascript doesn't know anything about Perl variables and can't interpolate them.

If you want the value of the $user perl var to be interpolated in that string, then Perl needs to be the one that does that interpolation before sending that data to the browser. In order to do that, the string that includes the $user var needs to be within double quotes or one of the other operators that allow interpolation.

Please show the surrounding perl code where that document.writeln statement is being used so we can show you how to correct it.


wickedxter
User

Jul 31, 2014, 9:13 PM

Post #5 of 38 (39250 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

you can do a ajax call to run the perl script to get the username and then update the DOM


terrykhatri531
User

Aug 2, 2014, 1:51 AM

Post #6 of 38 (39194 views)
Re: [wickedxter] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi Aaron,

Thanks for you reply, I have no clue how to do that, can you please help ?

Rgds.

Terry


FishMonger
Veteran / Moderator

Aug 2, 2014, 6:22 AM

Post #7 of 38 (39188 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

You haven't provided us with the info that I asked for.

If you're passing $user to index.pl via the query string, as you've shown, and index.pl is outputting the javascript statement you posted, then there's no logical reason to use an ajax call to another script to retrieve the same info you've already passed in the query string.

How is index.pl parsing the query string?

How and where are you declaring and assigning $user inside index.pl?

How are you outputting that javascript line?


terrykhatri531
User

Aug 3, 2014, 2:16 AM

Post #8 of 38 (39177 views)
Re: [FishMonger] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi Ron,

I am declaring parameter for $user at the top of my index.pl script :

# get form parameters
my $q = new CGI;
my $user = $q->param('user');


and assigning it to the following tag :



<ul class="tsc_pagination tsc_paginationA tsc_paginationA09">
<li><a href="#" class="$user">Welcome $user</a></li></ul>




I am just calling that javascript in index.pl which is actually for a jquery menu :

<div class="dtcodes_main">
<script type="text/javascript" src="dc_scripts/dreammenu.js"></script>




The very first line of that javascript which outputs "Go Back" and "Log out" where I am trying to call my $user variable is :


Code
  
document.writeln('<div class="dc-rnav"><a href="javascript:history.go(-1)" title="Go Back">Go Back</a> | <a href="login.pl" title="This will log you out">Log out</a> | <a href="" title="" >$user</a></div>');


I am trying other means to get that $user output just beside the "Log out" link where it show $user but not getting the x coordinate right, see the picture attached, like :


Code
  
<div style='float: right;'><a href="">$user</a></div>


If you like I can attach both index.pl and javascript, which will give you a better idea as to how to pass $user variable value to the javascript.

Many thanks for your help!!

Terry


(This post was edited by terrykhatri531 on Aug 3, 2014, 3:55 AM)
Attachments: User.png (2.13 KB)


FishMonger
Veteran / Moderator

Aug 3, 2014, 7:15 AM

Post #9 of 38 (39165 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post


Quote
If you like I can attach both index.pl and javascript


Are you saying that the javascript line that uses the perl var is a separate .js file? If so, then that is the obvious reason $user is not being interpolated in the javascript.

If you want to use a perl var in javascript code, then you need to have perl (index.pl) generate that javascript and interpolate the var. It can not be in a separate .js file.


terrykhatri531
User

Aug 3, 2014, 7:50 AM

Post #10 of 38 (39162 views)
Re: [FishMonger] How to pass variable value from perl to Javascript [In reply to] Can't Post

Thanks Ron,

Does anyone else have other ideas, suggestions, tricks or hacks to get it done ?

Many many thanks !!

Terry


FishMonger
Veteran / Moderator

Aug 3, 2014, 8:32 AM

Post #11 of 38 (39157 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

Have the perl script generate a hidden tag with the value of $user and have a javascript function that extracts the value of that tag by means of getElementById or getElementByName


Zhris
Enthusiast

Aug 3, 2014, 12:06 PM

Post #12 of 38 (39142 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

Other suggestions:

1) Don't use javascript to write <div class="dc-rnav"><a href="javascript:history.go(-1)" title="Go Back">Go Back</a> | <a href="login.pl" title="This will log you out">Log out</a> | <a href="" title="" >$user</a></div>. This is better placed with your HTML, the additional javascript layer is pointless.

2) Wrap the relevant javascript in a function, then call the function from your HTML supplying interpolated $user as an argument.

3) Treat the external .js file as a template with template placeholders / store its output in a Perl variable. Print this output between <script> tags.

Chris


wickedxter
User

Aug 3, 2014, 5:55 PM

Post #13 of 38 (39122 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

i hate mixing HTML with perl, use template toolkit so u can alter your HTML files with out haveing to alter your perl scripts.

index.pl

Code
#..normal stuff above 
use Template; #template toolkit


#point template to the folder
my $tt = Template->new({
INCLUDE_PATH => '/folder/to/',
EVAL_PERL => 1,
}) || die $Template::ERROR, "\n";


$tt->process('html_file.tt', {user => $cgi->param('user')}) || die $tt->error();



index.tt

Code
.. html ... 
<div class="dc-rnav"><a href="javascript:history.go(-1)" title="Go Back">Go Back</a> | <a href="login.pl" title="This will log you out">Log out</a> | <a href="" title="" >[% user %]</a></div>
..more html..


i think toolkit uses [% %] as default rather then <% %>


terrykhatri531
User

Aug 8, 2014, 9:58 AM

Post #14 of 38 (38875 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post


In Reply To
Other suggestions:

2) Wrap the relevant javascript in a function, then call the function from your HTML supplying interpolated $user as an argument.

That's what I would like to do, but instead of supplying interpolated $user as an argument in html I would rather save the $user value from first page when a user logs in, to a cookie and have javascript to read/retrieve the $user value from the cookie as I don't want to pass around $user variable from script to script as there are so many scripts in my application. If you find links which may help me do that, this will be really helpful. Thanks much !! Terry


(This post was edited by terrykhatri531 on Aug 8, 2014, 10:02 AM)


FishMonger
Veteran / Moderator

Aug 8, 2014, 10:46 AM

Post #15 of 38 (38863 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

The best solution is:
1) Store this type of var in a server side session variable so that all scripts can access it.

2) Use a template system such as Template Toolkit which is probably the best choice.

3) Use a MVC framework

4) Reduce or eliminate the javascript coding that generates html that should have been done on the server side, such as a set of navigation links.

Here's a navigation block I use in an app I'm currently developing using Template Toolkit.


Code
<div id="menubar"> 
[% IF session.user %]
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/inbox">Inbox</a></li>
<li><a href="/outbox">Outbox</a></li>
<li><a href="/partners">Partners</a></li>
<li><a href="/agreements">Agreements</a></li>
<li><a href="/certificates">Certificates</a></li>
<li><a href="/logout">Logout</a></li>
</ul>
[% ELSE %]
<h2>Please Login</h2>
[% END %]
</div> <!-- menubar -->

<div id="content">
<hr style="position: relative; top: 110px;">
[% content %]
</div> <!-- content -->



Zhris
Enthusiast

Aug 8, 2014, 7:34 PM

Post #16 of 38 (38801 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

reraised on Stack Overflow.

Fishmonger has given you great advice, you should use it.


Quote
I don't want to pass around $user variable from script to script as there are so many scripts in my application


I think I remember seeing a snippet of your code where you used CGI::Session to create a user session. This is where you could store data such as user. Pass this data to your HTML / javascript whenever you need it, in the same manner as when you pass the current page number to your pagination plugin.

As advised previously, you should break your application into functions, or atleast code that you need to regularly repeat, and put into an external file accessible by all your scripts. One of these functions could fetch user data and be called in each of your scripts i.e. my $user_data = fetch_user_data( @args ). We won't delve into object orientated Perl just yet!

Regards,

Chris


(This post was edited by Zhris on Aug 8, 2014, 8:08 PM)


FishMonger
Veteran / Moderator

Aug 8, 2014, 10:52 PM

Post #17 of 38 (38778 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hmm, Terry eq Tonya ?

Looks like we're dealing with a person suffering from dual personality.

Based on the threads I've seen from Terry/Tonya it appears that he/she is deliberately wanting to find the most difficult approach to a problem and wants us to fix the convoluted solution that he/she has decided on using.

Is it really worth wasting our time providing sound recommendations to a person that doesn't want or care about following those recommendations?


(This post was edited by FishMonger on Aug 8, 2014, 10:58 PM)


terrykhatri531
User

Aug 9, 2014, 12:00 AM

Post #18 of 38 (38771 views)
Re: [FishMonger] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi, Thanks for your advices, btw. Tonya Lepski is my girlfriend I am getting her to do postings at stackoverflow as I don't have an account there !! we're both just students.

Ron, My application is just about to finish and I do not want to do everything all over again but ofcourse for the next project I will be using the most recognized approaches and tools, so for now I am stuck with that jquery which is used globally through out in my application and trying to find a way to store $user name to a cookie and read it through that js script to be displayed on all the pages.

Thanks for your understanding !!

Rgds.

Terry


(This post was edited by terrykhatri531 on Aug 9, 2014, 12:25 AM)


terrykhatri531
User

Aug 9, 2014, 12:59 AM

Post #19 of 38 (38764 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post


In Reply To
reraised on Stack Overflow.

Fishmonger has given you great advice, you should use it. Indeed his and your advices are very valuable but I have gone a long way in this application I don't wanna change the framework like using Template Toolkit at this point, I do not wanna do everything all over again, but I am making notes of everything and saving links etc for my next project, as you must have realized this is my very first project !!


Quote
I don't want to pass around $user variable from script to script as there are so many scripts in my application


I think I remember seeing a snippet of your code where you used CGI::Session to create a user session. This is where you could store data such as user. Pass this data to your HTML / javascript whenever you need it, in the same manner as when you pass the current page number to your pagination plugin. Yes, you're right I am using it in my application but that java script is providing a template for my project and is used globally throughout the application, there are couple of issues with using CGI::Session for the purpose like I don't know how to pass variables stored in CGI Session to JS script or how to make JS script to read the value from CGI Session, and if I just use CGI Session to retrieve $user value and display it on the page using perl then given the framework provided by the JS script I cannot place it exactly where I want to do it, that is why I am sticking with that JS script, its way easier for me to use Perl and get helped as there are so many Gurus and Monks like you to always help me out.

As advised previously, you should break your application into functions, or atleast code that you need to regularly repeat, and put into an external file accessible by all your scripts. One of these functions could fetch user data and be called in each of your scripts i.e. my $user_data = fetch_user_data( @args ). We won't delve into object orientated Perl just yet! With this approach, again the problem is of placement/positioning of that information on the webpage, if I go for something outside of that JS script framework I cannot place where it should be, I have described that in one of my above comments, see the picture attached.

Thanks much !!Terry.
Attachments: User.png (2.13 KB)


terrykhatri531
User

Aug 9, 2014, 1:52 AM

Post #20 of 38 (38759 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi Chris,

Since you have read that thread at StarckOverflow, do you have any opinions as to why JS script fails to process my approach to read the contents of cookie and dislaunches itself ?

Thanks much !!

Terry


Zhris
Enthusiast

Aug 9, 2014, 4:03 AM

Post #21 of 38 (38749 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post


Quote
my $user = $q->param('user');
$user = "JAKE";
.....

`$.cookie("user", $user, { path:"/", expires:365 });`
`<script type="text/javascript" src="js/jquery.cookie.js"></script>`


Are you really trying to set the cookie using backticks (system call) directly from Perl?

To do it the way you want to, I guess my approach would be to write the header myself when I set the session as per the CGI::Session documentation, but set the session cookie as well as a user cookie. The user cookie should not be http only otherwise it won't be accessible by your javascript.


Code
my $user = 'Terry'; 

print $session->header(-location=>"http://192.168.56.105/perlproj/cgi-bin/index.pl?go=Details&user=$user&id=$id");

# minimum options set in this example, you'll have to set all those that are relevant to you.
my $session_cookie = CGI::Cookie->new(-name=>$session->name, -value=>$session->id, -httponly=>1);
my $user_cookie = CGI::Cookie->new(-name=>'user', -value=>$user, -httponly=>0);

print $cgi->redirect(-uri => "http://192.168.56.105/perlproj/cgi-bin/index.pl?go=Details&user=$user&id=$id", -cookie=>[$session_cookie, $user_cookie]);



Code
var user = $.cookie('user'); 

document.writeln('<div class="dc-rnav"><a href="javascript:history.go(-1)" title="Go Back">Go Back</a> | <a href="login.pl" title="This will log you out">Log out</a> | <a href="" title="" >' + user+ '</a></div>');


You could of course set the cookie using the javascript plugin as you have already attempted to do, but it would probably be a little trickier to implement. You would need to do it on the page you redirect to after logging in, which would require fetching the user from the session, it would be poor practice to fetch from the user param.

Chris


(This post was edited by Zhris on Aug 9, 2014, 4:41 AM)


Zhris
Enthusiast

Aug 9, 2014, 4:21 AM

Post #22 of 38 (38745 views)
Re: [FishMonger] How to pass variable value from perl to Javascript [In reply to] Can't Post

You're absolutely right, it does appear that a lot of advice doesn't get applied. I think this is partly down to lack of both understanding and experience, Terry has his mind set on what he thinks is the quickest / easiest solution, but we know otherwise. For a first project, he has most definitely jumped into the deep end of the pool. If Terry provided standalone programs (not always possible) that represent his problems to aid his descriptions of the problems, I would be much more forgiving due to this effort. When we understand the problem itself, but not necessarily the approach he has taken to solve the problem, we can only provide advice, good advice.

Chris


(This post was edited by Zhris on Aug 9, 2014, 4:25 AM)


terrykhatri531
User

Aug 9, 2014, 4:38 AM

Post #23 of 38 (38738 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post

 Thanks Chris,

No backticks in the script, some formatting issues while posting.

I tried var user = $.cookie('user'); within the JS Script but its not working and it takes the JS Menu off the page.

Any other ideas like somehow wrapping it up within a function and push it ?

Many thanks !

Terry


Zhris
Enthusiast

Aug 9, 2014, 4:43 AM

Post #24 of 38 (38736 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

Write a standalone .html that represents this problem and post the code here (attach any relevant .js). Use a javascript debugger such as firefox firebug to check for javascript errors. If there are errors, then its possible the menu won't get written.

E.g.:


Code
<html> 
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/jquery.cookie/1.4.1/jquery.cookie.js"></script>
<script type="text/javascript">
$.cookie('user', 'terry', { expires: 7, path: '/' });
</script>
<script type="text/javascript">
var user = $.cookie('user');

document.writeln('<div class="dc-rnav"><a href="javascript:history.go(-1)" title="Go Back">Go Back</a> | <a href="login.pl" title="This will log you out">Log out</a> | <a href="" title="" >' + user + '</a></div>');
</script>
</head>
<body>
</body>
</html>


Chris


(This post was edited by Zhris on Aug 9, 2014, 5:06 AM)


terrykhatri531
User

Aug 9, 2014, 6:53 AM

Post #25 of 38 (38718 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi Chris,

That will work, but I want to do is to read and assign user name from cookie from within the JS SCRIPT so the links are properly positioned on the webpage, and that's what I have been asking around and that's what I am trying to do. if I print those links the way you have done these links will not be properly aligned at the top right hand corner just above the menu bar end, please see the picture attached in my above comment.

Attached is the html file with all the referenced scripts and css files.

I don't know what I am trying to do is possible or not !!

Thanks for your help though !

Terry
Attachments: jquery.tar.gz (39.5 KB)


Zhris
Enthusiast

Aug 9, 2014, 7:13 AM

Post #26 of 38 (18059 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi Terry,

I didn't intend for you to use that example as it stands, I just included it to represent a standalone html file. I also thought it may be useful for you to see a working example that makes use of the jquery cookie plugin.

Bit busy right now (my cousin is coming to stay so have to tidy up :(). I will take a look later on.

Regards,

Chris


Zhris
Enthusiast

Aug 9, 2014, 9:56 AM

Post #27 of 38 (18045 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi Terry,

The first thing I did was check for errors using firebugs console. I encountered the following:

1) jquery.cookie.js was missing, I'm sure you just forgot to include it, if not then you will need to.

2) the common "$ is not defined" error occurred, this is because you didn't include jquery before you start using it. The very first script you should include should be jquery.

Upon re-running I noticed that the user variable wasn't interpolated by javascript properly. This was because you had some odd escape sequences in your nav bar code in dreammenu.js. You need to remove these backslashes i.e. \' + user + \' should be ' + user + '.

Fix these three issues, then let us know if you have any further issues (provide all relevant code again). There are a tonne of other improvements I could discuss, but I don't think its necessary. I'm also still not certain on how you plan to set that user cookie, your current method probably won't be appropriate, depending.

Regards,

Chris


(This post was edited by Zhris on Aug 9, 2014, 10:04 AM)


terrykhatri531
User

Aug 9, 2014, 10:32 AM

Post #28 of 38 (18033 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post

Thanks Chris, Yes the jquery.cookie.js script is missing from the attachment I sent you, its attached now, I am looking into the issues you mentioned.

Many thanks !

Terry
Attachments: jquery.cookie.js (3.05 KB)


wickedxter
User

Aug 9, 2014, 8:54 PM

Post #29 of 38 (18011 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post


Code
<HTML> 
<HEAD>
<TITLE> Cookie Issue </TITLE>
<link type="text/css" rel="stylesheet" href="dreamcodes.css" />
<link type="text/css" rel="stylesheet" href="dreamtooltips.css" />
<script type="text/javascript" src="dreamtooltips.js"></script>
<script type="text/javascript" src="dreamcodes.js"></script>
<script type="text/javascript" src="jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="jquery.cookie.js"></script>
</HEAD>
<BODY>
<div class="dtcodes_main">
<script type="text/javascript" src="dreammenu.js"></script>
<noscript>
Navigation menu requires Javascript to be enabled.
</noscript>

<h1>Script dreammenu.js is the one where I am trying to read and assign user value from cookie</h1>

<script type="text/javascript">
//$.cookie('user', 'Terry', { path:'/', expires:1 });
$(document).ready(function() {

$('#user').text($.cookie('user'));
});
</script>
</div>
</BODY>
</HTML>


this is the index.html page


Code
<a href="" title="" id="user">\'+ user+ \'</a>


this is the new link in the dreammenu.js, i added the id so jquery will update the link text to the cookie.


(This post was edited by wickedxter on Aug 9, 2014, 9:00 PM)


terrykhatri531
User

Aug 10, 2014, 3:14 AM

Post #30 of 38 (18003 views)
Re: [wickedxter] How to pass variable value from perl to Javascript [In reply to] Can't Post

Thanks Aaron ! Appreciate it !!

Terry


terrykhatri531
User

Aug 10, 2014, 3:45 AM

Post #31 of 38 (18001 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post

Thanks Chris !!

I am having problems with my routines to create cookie, I am adding the routines in my script where I think it should be but its giving me a malformed header error, but the header is printed already in the portion of the script which gets loaded first:


Code
 
malformed header from script. Bad header=<script type="text/javascript": login.pl, referer: http://192.168.56.105/perlproj/cgi-bin/login.pl




Here is the relevant snippet of the code :


Code
 
if ( ($action eq "LOGIN") && ($empid =~ /\d+/)) {
if ($upass eq $password) {
$msg = "Login successful - redirecting !!!";
$session = new CGI::Session();
$session->expire("24h");
$session->param(user => $user);

#Delete the previouce cookie
print qq!<script type="text/javascript">
\$.cookie('user',null,{path:'/'});
</script>!;

#Set new cookie
print qq!<script type="text/javascript">
\$.cookie('user', $user, { path:'/', expires:1 }) ;
</script>!;

print $session->header(-location=>" http://192.168.56.105/perlproj/cgi-bin/index.pl?go=Details&user=$user&id=$id");

} else {
$msg = "Wrong password";

}

} else {
$msg = "Please complete form";
}


I have attached the full perl script, so when you have time please have a look at it.

Many many thanks !!

Terry
Attachments: login.pl (4.66 KB)


Zhris
Enthusiast

Aug 10, 2014, 4:55 AM

Post #32 of 38 (17995 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi Terry,

That isn't going to work.

The first thing you should print is the header. You have attempted to print html / javascript before the header. Also, you are using a redirect header, therefore anything you print after it will be ignored anyway (I think, I have never tried).

I provided an example of creating the cookies using CGI::Cookie, then setting them when you create the redirect header a few posts ago (post #21). Non http only cookies can be shared between Perl and javascript. Try implementing this, post it you get stuck. If this isn't suitable for some reason then you'll have to change your application design i.e. remove the redirect, or set the cookies in index.pl.

Also don't worry about deleting the cookie, when you set the cookie it will create if it doesn't exist or update the existing.

Chris


(This post was edited by Zhris on Aug 10, 2014, 4:58 AM)


terrykhatri531
User

Aug 10, 2014, 5:17 AM

Post #33 of 38 (17988 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post


In Reply To
Hi Terry,

That isn't going to work.

The first thing you should print is the header. You have attempted to print html / javascript before the header. Also, you are using a redirect header, therefore anything you print after it will be ignored anyway (I think, I have never tried). You're right if I print header again, the redirecting after that does not work, so you are saying I am stuck on this one, don't you have any tricks up your sleeves Smile ThanksTerry


Zhris
Enthusiast

Aug 10, 2014, 5:21 AM

Post #34 of 38 (17986 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

A solution is in my previous comment / post #21 Sly.

Another alternative, but not as clean, would be to create an intermediate screen. Instead of redirecting, set the cookie using jquery (or in Perl header), print the message "Logging you in...", then use a html meta refresh (or a javascript method) to redirect to index.pl after a set number of seconds.

Chris


(This post was edited by Zhris on Aug 10, 2014, 5:59 AM)


terrykhatri531
User

Aug 10, 2014, 5:53 AM

Post #35 of 38 (17975 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post

Cool !! thanks ! You're amazing !!

Terry


Zhris
Enthusiast

Aug 10, 2014, 5:55 AM

Post #36 of 38 (17973 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

Thats what she said Wink.


terrykhatri531
User

Aug 10, 2014, 6:17 AM

Post #37 of 38 (17966 views)
Re: [Zhris] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi Chris,

One issue I noticed, if I login again in a different tab of the same browser the user gets overwritten and it displays the last user in both tabs, meaning I cannot maintain 2 sessions within the same browser using different users.

I can use different cookie name like I can create cookies with $id variable but there will be problems with javascript.

Any work around ?

Many thanks !!

Terry


Zhris
Enthusiast

Aug 10, 2014, 7:00 AM

Post #38 of 38 (17961 views)
Re: [terrykhatri531] How to pass variable value from perl to Javascript [In reply to] Can't Post

Hi Terry,

This is a pretty good question. Its something I have thought about in the past, but never implemented a solution.

Different tabs in the same browser will share the same cookie under the same name as you have gathered. This is usually the desired behavior.

Firstly there are browser add ons that you can use to maintain 2 separate sessions with ease in the same browser. But inevitably you can't expect your users to know about these, have them installed, or use them.

I can think of a few possible ways a solution could be implemented. A scenario I would look into would be to append a unique id on the end of the cookie name, perhaps the user id or just an incrementing integer, on login. The state of this id will need to be maintained, probably by passing it around in every link via query string and every form via hidden field, ensuring each tab can maintain its own id. Obviously you won't be able to maintain the id in your session if you need the cookie to read the session beforehand. Build the cookie name every time you need to access it i.e. my $cookie_name = $cookie_name_prefix . $cookie_name_id;, validate it exists, then do whatever. Both Perl and javascript will be capable of handling this process. However, if your user types in the URL themselves and doesn't pass this id, then validation will fail, although you could always set a fallback cookie with a shared name.

Perhaps somebody else will have a better suggestion.

Regards,

Chris


(This post was edited by Zhris on Aug 10, 2014, 7:10 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives