8 messages in com.mysql.lists.perlGiving Javascript code access to Perl...
FromSent OnAttachments
J Wermont15 Oct 2005 17:13 
Tony Dean16 Oct 2005 09:20 
Felipe Gasper16 Oct 2005 10:10 
Eric17 Oct 2005 15:09 
J Wermont17 Oct 2005 15:27 
Christopher Pryce17 Oct 2005 18:38 
Felipe Gasper17 Oct 2005 18:51 
J Wermont17 Oct 2005 19:37 
Subject:Giving Javascript code access to Perl variables
From:J Wermont (jwer@sonic.net)
Date:10/15/2005 05:13:42 PM
List:com.mysql.lists.perl

In a .CGI file, I'd like to include some Javascript code (which will be written to the output file sent to the client's browser), which will access Perl variables that were set in the same CGI file.

It seems to work in some cases. For example, I have a CGI file that includes 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 ...

ENDOFPAGE

When this page is displayed in the browser, you see:

Hello world Hello world Hello world Hello world

So clearly the Javascript code recognizes the Perl variable $loopcount.

Can you do this with Perl array values, too? So far I haven't had any success. Can anyone recommend a good site (or book) that addresses this issue?

Here's my reason: I'm using Javascript to validate an HTML form, and I want it to be done on the client side. This works fine for checking invalid types of input (eg, numbers where letters should go, etc), but I also would like to make sure that what is being typed into a text input field is not a duplicate of something I already have stored in a database.

Since I can access the database from my Perl script and store the values in a Perl array variable, I'm thinking I could then download the array to the client, so that the Javascript code can compare typed input with values in the array. Since I was able to download the value "4" in the Perl variable $loopcount, shouldn't I be able to download an array of values to the client?

(By the way, this is a *very tiny* array of database values. If it involved a large amount of data, I would check for duplicates on the server, after the form was submitted, since I imagine the time it would take to download all that data to the browser would undermine any performance savings you get by doing client-side checking.)

Hope this makes sense. Maybe I should post this to a Javascript group as well, but I'm hoping that some of you might be familiar with both languages and can advise me.

Thanks!