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

 

First page Previous page 1 2 Next page Last page  View All


Zhris
Enthusiast

Aug 9, 2014, 7:13 AM

Post #26 of 38 (980 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 (966 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 (954 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 (932 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 (924 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 (922 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 (916 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 (909 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 (907 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 (896 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 (894 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 (887 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 (882 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)

First page Previous page 1 2 Next page Last page  View All
 
 


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

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