469,330 Members | 1,285 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

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

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
10 5658
eWish
971 Expert 512MB
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
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
971 Expert 512MB
Where are you getting the value of $var? Is it user input?

--Kevin
Mar 18 '08 #4
eWish..

Yes, It is the input variable I am getting from the database.
Mar 18 '08 #5
eWish
971 Expert 512MB
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
Thank you eWish for you efforts to help me out...

Some one plzz...help me
Mar 18 '08 #7
KevinADC
4,059 Expert 2GB
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
arggg
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
4,059 Expert 2GB
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
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.

Similar topics

2 posts views Thread by pleaseexplaintome | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.