Apache2-Ajax

 view release on metacpan or  search on metacpan

lib/Apache2/Ajax.pm  view on Meta::CPAN

sub PJX_DEBUG {
   my ($self, $parms, $arg1) = @_;
   $self->{DEBUG} = $arg1;
}

1;

__END__

=head1 NAME

Apache2::Ajax - mod_perl interface to CGI::Ajax

=head1 SYNOPSIS

  ######################################################
  # in httpd.conf
  PerlLoadModule Apache2::MyAjaxApp
  <Location /ajax>
     SetHandler perl-script
     PerlResponseHandler Apache2::MyAjaxApp
     PJX_fn js_function_name perl_function_name
     PJX_html Show_Form_sub
     PJX_JSDEBUG 2
     PJX_DEBUG 1
  </Location>
  ######################################################
  
  ######################################################
  # module file Apache2/MyAjaxApp.pm
  package Apache2::MyAjaxApp
  use Apache2::Ajax;
  # use whatever else
  
  sub perl_function_name {
    my @params = @_;
    # do whatever
    return $return_value;
  }
  
  sub Show_Form_sub {
    my $html = '';
    # construct html string
    return $html;
  }
  
  sub handler {
    my $r = shift;
    # do stuff
    my $ajax = Apache2::Ajax->new($r);
    $r->print($ajax->build_html());
    return Apache2::Const::OK;
  }
  1;
  ##################################################

=head1 DESCRIPTION

This module is a mod_perl2 interface to L<CGI::Ajax>,
which provides a mechanism for using perl code
asynchronously from javascript-enhanced HTML pages.

As well as L<mod_perl2>,
this package requires L<CGI::Ajax>, as well as a CGI.pm-compatible
CGI module for supplying the I<param()> and I<header()> methods.
If available, L<CGI::Apache2::Wrapper> will be used,
which is a minimal module that uses methods of L<mod_perl2>
and L<Apache2::Request> to provide these methods; if this
is not available, L<CGI> (version 2.93 or greater) will be used.

Setting things up can be illustrated by the following
example of L<CGI::Ajax>, which contains a more thorough
discussion, as well as a number of illustrative example
scripts.

=over

=item * Define a Perl subroutine

At least one Perl subroutine must be defined which
takes input from some form element and returns a
result. For example,

   sub evenodd_func {
     my $input = shift;
     # check that $input is defined and is a number
     $input % 2 == 0 ? return("EVEN") : return("ODD");
   }

will accept an argument from a form element and
return a string indicating if that number is even or odd.

Note that, in this module, only Perl subroutines
are used, whereas in L<CGI::Ajax>, references to
subroutines may also be used.

=item * Generate the web page

A subroutine is provided which generates the html for the
web page. Within this is a trigger which calls the Perl subroutine,
as well as the particular HTML div element to which the results
are sent. For example,

  sub Show_HTML {
    my $html = <<EOT;
  <HTML>
  <HEAD><title>A Simple Example</title>
  </HEAD>
  <BODY>
    Enter a number:&nbsp;
    <input type="text" name="somename" id="val1" size="6"
       OnKeyUp="evenodd( ['val1'], ['resultdiv'] );">
    <br>
    <hr>
    <div id="resultdiv">
    </div>
  </BODY>
  </HTML>
  EOT

    return $html;



( run in 1.939 second using v1.01-cache-2.11-cpan-9581c071862 )