CGI-Ajax
view release on metacpan or search on metacpan
lib/CGI/Ajax.pm view on Meta::CPAN
results to the web page, and supports returning values to multiple
DIV elements on the HTML page.
Using CGI::Ajax, the URL for the HTTP GET/POST request is
automatically generated based on HTML layout and events, and the
page is then dynamically updated with the output from the perl
function. Additionally, CGI::Ajax supports mapping URL's to a
CGI::Ajax function name, so you can separate your code processing
over multiple scripts.
Other than using the Class::Accessor module to generate CGI::Ajax'
accessor methods, CGI::Ajax is completely self-contained - it
does not require you to install a larger package or a full Content
Management System, etc.
We have added I<support> for other CGI handler/decoder modules,
like L<CGI::Simple> or L<CGI::Minimal>, but we can't test these
since we run mod_perl2 only here. CGI::Ajax checks to see if a
header() method is available to the CGI object, and then uses it.
If method() isn't available, it creates it's own minimal header.
A primary goal of CGI::Ajax is to keep the module streamlined and
maximally flexible. We are trying to keep the generated javascript
code to a minimum, but still provide users with a variety of
methods for deploying CGI::Ajax. And VERY little user javascript.
=head1 EXAMPLES
The CGI::Ajax module allows a Perl subroutine to be called
asynchronously, when triggered from a javascript event on the
HTML page. To do this, the subroutine must be I<registered>,
usually done during:
my $pjx = new CGI::Ajax( 'JSFUNC' => \&PERLFUNC );
This maps a perl subroutine (PERLFUNC) to an automatically
generated Javascript function (JSFUNC). Next you setup a trigger this
function when an event occurs (e.g. "onClick"):
onClick="JSFUNC(['source1','source2'], ['dest1','dest2']);"
where 'source1', 'dest1', 'source2', 'dest2' are the DIV ids of
HTML elements in your page...
<input type=text id=source1>
<input type=text id=source2>
<div id=dest1></div>
<div id=dest2></div>
L<CGI::Ajax> sends the values from source1 and source2 to your
Perl subroutine and returns the results to dest1 and dest2.
=head2 4 Usage Methods
=over 4
=item 1 Standard CGI::Ajax example
Start by defining a perl subroutine that you want available from
javascript. In this case we'll define a subrouting that determines
whether or not an input is odd, even, or not a number (NaN):
use strict;
use CGI::Ajax;
use CGI;
sub evenodd_func {
my $input = shift;
# see if input is defined
if ( not defined $input ) {
return("input not defined or NaN");
}
# see if value is a number (*thanks Randall!*)
if ( $input !~ /\A\d+\z/ ) {
return("input is NaN");
}
# got a number, so mod by 2
$input % 2 == 0 ? return("EVEN") : return("ODD");
}
Alternatively, we could have used coderefs to associate an
exported name...
my $evenodd_func = sub {
# exactly the same as in the above subroutine
};
Next we define a function to generate the web page - this can
be done many different ways, and can also be defined as an
anonymous sub. The only requirement is that the sub send back
the html of the page. You can do this via a string containing the
html, or from a coderef that returns the html, or from a function
(as shown here)...
sub Show_HTML {
my $html = <<EOT;
<HTML>
<HEAD><title>CGI::Ajax Example</title>
</HEAD>
<BODY>
Enter a number:
<input type="text" name="somename" id="val1" size="6"
OnKeyUp="evenodd( ['val1'], ['resultdiv'] );">
<br>
<hr>
<div id="resultdiv">
</div>
</BODY>
</HTML>
EOT
return $html;
}
The exported Perl subrouting is triggered using the C<OnKeyUp>
event handler of the input HTML element. The subroutine takes one
value from the form, the input element B<'val1'>, and returns the
the result to an HTML div element with an id of B<'resultdiv'>.
Sending in the input id in an array format is required to support
multiple inputs, and similarly, to output multiple the results,
you can use an array for the output divs, but this isn't mandatory -
as will be explained in the B<Advanced> usage.
Now create a CGI object and a CGI::Ajax object, associating a reference
to our subroutine with the name we want available to javascript.
my $cgi = new CGI();
my $pjx = new CGI::Ajax( 'evenodd' => \&evenodd_func );
And if we used a coderef, it would look like this...
my $pjx = new CGI::Ajax( 'evenodd' => $evenodd_func );
Now we're ready to print the output page; we send in the cgi
object and the HTML-generating function.
( run in 1.163 second using v1.01-cache-2.11-cpan-9581c071862 )