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


terrykhatri531
User

Jul 30, 2014, 7:41 PM

Post #1 of 38 (2906 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 (2898 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 (2893 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 (2887 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 (2866 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 (2810 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 (2804 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 (2793 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 (2781 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 (2778 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 (2773 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 (2758 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 (2738 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 (2491 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 (2479 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 (2417 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 (2394 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 (2387 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 (2380 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 (2375 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 (2365 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 (2361 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 (2354 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 (2352 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 (2334 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)

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