Apache2-Ajax

 view release on metacpan or  search on metacpan

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

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;
  }

By means of either an Apache configuration directive
or arguments passed into the constructor for the
object, to be described later, the Perl subroutine
I<evenodd_func> defined earlier will be associated
with a JavaScript function I<evenodd>. This function
is triggered using the I<OnKeyUp> event handler of the
input HTML element. The subroutine
takes one value from the form, the input element I<val1>,
and returns the the result to an HTML div element with an id
of I<resultdiv>.

=back

There may be circumstances under which it is desireable
to generate the html page directly within a handler, rather
than through a subroutine as described above. This is
possible, but one is then responsible for inserting
the JavaScript code directly into the page, which can be
done with the I<show_javascript()> method described later.
The following is a a handler which illustrates this technique:

  sub handler {
    my $r = shift;
    my $my_func = sub {
      my $arg = shift;
      return ( $arg . " with some extra" );
    };
    my $ajax = Apache2::Ajax->new($r, tester => $my_func);
    my $html = "";
    $html .= "<HTML>";
    $html .= "<HEAD>";
    
    $html .= $ajax->show_javascript;
    
    $html .= <<EOT;
    </HEAD>
    <BODY>
    <FORM name="form">
    <INPUT type="text" id="inarg"
        onkeyup="tester(['inarg'],['output_div']); return true;">
    <hr>
    <div id="output_div"></div>
    </FORM>
    <br/><div id='pjxdebugrequest'></div><br/>
    </BODY>
    </HTML>
    EOT
    
    my $cgi = $ajax->cgi;
    my $pjx = $ajax->pjx;
    $cgi->header();
    
    if ( not $cgi->param('fname') ) {
      $r->print($html);
    }
    else {
      $r->print($pjx->handle_request());
    }
    return Apache2::Const::OK;
  }

=head1 mod_perl handler

The mod_perl response handler used must use
I<Apache2::Ajax>, and has the following general form:

  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, %new_args);
    $r->print($ajax->build_html(%build_args));
    return Apache2::Const::OK;
  }

Apache2::Ajax makes available the following.

=head2 Methods

=over

=item my $ajax = Apache2::Ajax->new($r, %new_args);

The I<new()> method creates an I<Apache2::Ajax> object,



( run in 1.930 second using v1.01-cache-2.11-cpan-13bb782fe5a )