Re: Giving Javascript code access to Perl variables

Eric <[email protected]>
Newsgroups gmane.comp.db.mysql.perl
Message-ID <[email protected]>
Javascript is not actualy reading the value from perl. When you print out
the javascript it is inserting the value from the variable in perl into that
spot in the string. If you had used single quotes it would not work at all.
The moral, don't try to communicate between the two. Instead make your perl
output dynamic javascript. Yea that sounds crazy, but you can generate the
correct javascript for a specific case. So you do kind of get one way
communication, but realy in the end, if you view the javasript source it has
no idea that those values came from perl.

On 10/16/05, Felipe Gasper <[email protected]> wrote:
>
> 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]
>
>


--
--
__________
Eric Hodges
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.