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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • getmadhu
    New Member
    • Mar 2008
    • 11

    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
    ----------------
    Code:
    $var = 'sample'
    <select name="sample" onChange="lbChange('sample');">
    instead of above I want to pass the perl var

    Code:
    <select name="sample" onChange="lbChange($var);">
    Javascript
    ---------------
    Code:
    function lbChange(lbName) {
       if (lbName == 'sample') {
    alert ("Value:"+ lbname);
    }
    in the above function how can I retreive the $var of perl
    Last edited by eWish; Mar 18 '08, 03:02 PM. Reason: Added Code tags
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    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

    Comment

    • getmadhu
      New Member
      • Mar 2008
      • 11

      #3
      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

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        Where are you getting the value of $var? Is it user input?

        --Kevin

        Comment

        • getmadhu
          New Member
          • Mar 2008
          • 11

          #5
          eWish..

          Yes, It is the input variable I am getting from the database.

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            You could include this line in your script and print it to the browser after the value has been populated.

            Code:
            <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

            Comment

            • getmadhu
              New Member
              • Mar 2008
              • 11

              #7
              Thank you eWish for you efforts to help me out...

              Some one plzz...help me

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by getmadhu
                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.

                Comment

                • arggg
                  New Member
                  • Mar 2008
                  • 91

                  #9
                  Originally posted by KevinADC
                  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.

                  Code:
                  echo "<a href=\"#\" onClick=\"javascript:Test('".$PHP_VAR."');>Link</a>";

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Originally posted by arggg
                    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.

                    Code:
                    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.

                    Comment

                    • getmadhu
                      New Member
                      • Mar 2008
                      • 11

                      #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="javascrip t">
                      initParam($var)
                      </script>

                      Comment

                      Working...