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: Re: [terrykhatri531] How to pass variable value from perl to Javascript: Edit Log



Zhris
Enthusiast

Aug 9, 2014, 4:03 AM


Views: 38694
Re: [terrykhatri531] How to pass variable value from perl to Javascript


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)


Edit Log:
Post edited by Zhris (Enthusiast) on Aug 9, 2014, 4:41 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