Re: Giving Javascript code access to Perl variables
Felipe Gasper <[email protected]>
| Newsgroups | gmane.comp.db.mysql.perl |
|---|---|
| Message-ID | <[email protected]> |
I'd say the best way to do this is the JSON module on CPAN.
-FG
Quoth J Wermont on 10/15/2005 8:14 PM...
> 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
>
>
--
Felipe M. L. Gasper, conductor/singer
http://felipegasper.com
Judge ideas, not people.
Love people, not ideas.
--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/[email protected]