Re: Giving Javascript code access to Perl variables

"Tony Dean" <[email protected]>
Newsgroups gmane.comp.db.mysql.perl
Message-ID <000501c5d26d$8eb4a9c0$cb24ff3e@ajd>
The simplest approach, as I see it, is just to interrupt the print of the JS
code at the point you wish to make the declaration.

my (@array) = ('now','is','the','time','for','all','good','men');
my (@new, $tmp, $val);

for $val (@array) { push @new, "'$val'";} # Ensure quotes are present
$tmp = join ',', @new;

# Print bulk JS code

print "var array_name = new Array($tmp);"; # Now in JS style

# Print more JS code

Hope this helps... Tony Dean

----- Original Message ----- 
From: "J Wermont" <[email protected]>
To: <[email protected]>
Sent: Sunday, October 16, 2005 1:14 AM
Subject: Giving Javascript code access to Perl variables


> 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!
>
> J. Wermont
>
>
> -- 
> MySQL Perl Mailing List
> For list archives: http://lists.mysql.com/perl
> To unsubscribe:    http://lists.mysql.com/[email protected]
>
>


-- 
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe:    http://lists.mysql.com/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.