By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

How can i pass and access a Perl var parameter in a Javascript function

P: 11
Hi All,

How can i pass a Perl var($val) as a parameter to a Javascript function.

Please can any one help me out...

ex:
Perl Code
----------------
Expand|Select|Wrap|Line Numbers
  1. $var = 'sample'
  2. <select name="sample" onChange="lbChange('sample');">
instead of above I want to pass the perl var

Expand|Select|Wrap|Line Numbers
  1. <select name="sample" onChange="lbChange($var);">
Javascript
---------------
Expand|Select|Wrap|Line Numbers
  1. function lbChange(lbName) {
  2.    if (lbName == 'sample') {
  3. alert ("Value:"+ lbname);
  4. }
  5.  
in the above function how can I retreive the $var of perl
Mar 18 '08 #1
Share this Question
Share on Google+
10 Replies


eWish
Expert 100+
P: 971
You might be able to use a templating system that would allow you to embed perl code in the template file that could handle this for you. Check at CPAN for a templating system like Mason or Template Toolkit.

--Kevin
Mar 18 '08 #2

P: 11
Thanks Kevin for the response...

Is there any other way with out using the CPAN module.. because I already wrote a huge no. of lines of code(Perl/CGI/HTML/Javascript).. So, I don't want to change the whole thing. It will mess up completely....

I searched a lot to get that.. still I am unable to get the sol.. If any one help me this. really I will be very thank full to him
Mar 18 '08 #3

eWish
Expert 100+
P: 971
Where are you getting the value of $var? Is it user input?

--Kevin
Mar 18 '08 #4

P: 11
eWish..

Yes, It is the input variable I am getting from the database.
Mar 18 '08 #5

eWish
Expert 100+
P: 971
You could include this line in your script and print it to the browser after the value has been populated.

Expand|Select|Wrap|Line Numbers
  1. <select name="sample" onChange="lbChange($var);">
But I don't know of a way other than a templating system or refreshing of the browser to achieve what you are after. Maybe someone else will.

--Kevin
Mar 18 '08 #6

P: 11
Thank you eWish for you efforts to help me out...

Some one plzz...help me
Mar 18 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
Thank you eWish for you efforts to help me out...

Some one plzz...help me
The short answer is you can't. Javascript executes in the browser while perl code executes on the remote server. The only way they communicate is via CGI forms. Now your perl code can print the javascript code and fill in values in the javascript using perl variables up to the time the javascript is printed out. After that you have to send data back to the perl script to rewrite the javascript. If you wanted to do that without reloading the entire page, you want to look into AJAX. I have no experience with AJAX so I can't help with code.
Mar 18 '08 #8

P: 91
The short answer is you can't. Javascript executes in the browser while perl code executes on the remote server. The only way they communicate is via CGI forms. Now your perl code can print the javascript code and fill in values in the javascript using perl variables up to the time the javascript is printed out. After that you have to send data back to the perl script to rewrite the javascript. If you wanted to do that without reloading the entire page, you want to look into AJAX. I have no experience with AJAX so I can't help with code.
Is perl creating the html? If so can't you just have it print out the variable just like as if you were using PHP? For example.

Expand|Select|Wrap|Line Numbers
  1. echo "<a href=\"#\" onClick=\"javascript:Test('".$PHP_VAR."');>Link</a>";
Mar 18 '08 #9

KevinADC
Expert 2.5K+
P: 4,059
Is perl creating the html? If so can't you just have it print out the variable just like as if you were using PHP? For example.

Expand|Select|Wrap|Line Numbers
  1. echo "<a href=\"#\" onClick=\"javascript:Test('".$PHP_VAR."');>Link</a>";
Did you read what I posted? Plus that is what eWish already posted too. But I guess it's cool to post it again just in case the OP hasn't gotten the message yet.
Mar 18 '08 #10

P: 11
Hi frnds,

Thanks a lot.. Problem got resolved... I just sent the $var to javascript function as an argument and taken into a js variable.

<script type="javascript">
initParam($var)
</script>
Mar 19 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.