Jump to content

Giving Javascript code access to Perl variables


jwermont

Recommended Posts

Hello,In a .CGI file, I'd like to include some Javascript code (which will bewritten to the output file sent to the client's browser), which willaccess Perl variables that were set in the same CGI file.It seems to work in some cases. For example, I have a CGI file thatincludes this code: #!/usr/local/bin/perl ... Perl code ... $loopcount = 4; ... more Perl code ... print << "ENDOFPAGE"; ... HTML code ... <script language="Javascript" type="text/javascript"> for (i=0; i<$loopcount; i++) { document.write ("Hello world<br>") } </script> ... more HTML ... ENDOFPAGEWhen this page is displayed in the browser, you see:Hello worldHello worldHello worldHello worldSo clearly the Javascript code recognizes the Perl variable $loopcount,or the value of $loopcount was made available during the execution ofthe Javascript.Can you do this with Perl array values, too? So far I haven't had anysuccess. Can anyone recommend a good site (or book) that addresses thisissue?Here's my reason: I'm using Javascript to validate an HTML form, andI want it to be done on the client side. This works fine for checkinginvalid types of input (eg, numbers where letters should go, etc), butI also would like to make sure that what is being typed into a textinput field is not a duplicate of something I already have stored ina database.Since I can access the database from my Perl script and store the valuesin a Perl array variable, I'm thinking I could then download the arrayto the client, so that the Javascript code can compare typed input withvalues in the array. Since I was able to download the value "4" in thePerl variable $loopcount, shouldn't I be able to download an array ofvalues to the client?(By the way, this is a *very tiny* array of database values. If itinvolved a large amount of data, I would check for duplicates on theserver, after the form was submitted, since I imagine the time itwould take to download all that data to the browser would undermineany performance savings you get by doing client-side checking.)Hope this makes sense. Maybe I should post this to a Javascript groupas well, but I'm hoping that some of you might be familiar with bothlanguages and can advise me.Thanks!J. Wermont

Link to comment
Share on other sites

  • 2 weeks later...

Nice use of the "here doc".You could format a string and pass it to the js split():

#set the PERL var$array_str = '1,2,3,4,5';print header;print start_html(-title=>'JS->PERL CGI');print h2("Here's a PERL variable:");print <<HTML;<table border="1">       <tr>       <script type="text/javascript">       var myStr = '$array_str';       var js_array = myStr.split(",");           for(var j = 0; j < js_array.length; j++){            document.write("<td>" + js_array[j] + "</td>");            }       </script>       </tr></table>HTML

Thanks,

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...