Solution:
In the previous post, no. 24, i tried to show how to use CGI::Ajax with perl code. Now i am going to use Apache2::Ajax which is mod_perl interface to CGI::Ajax.
This module require mod_perl2, CGI::Ajax, as well as a CGI.pm-compatible CGI module for supplying the param() and header() methods. If available, CGI::Apache2::Wrapper will be used, which is a minimal module that uses methods of mod_perl2 and Apache2::Request to provide these methods; if this is not available, CGI (version 2.93 or greater) will be used.
To use this module you have to make some changes in your httpd.conf, which is located at /etc/httpd/conf/.
To make the use of the below example, some steps need to be followed:-
(i) Create a directory cgi-bin2 in /var/www/.
(ii) Following change must be done your httpd.conf :-
PerlModule ModPerl::Registry
Alias /cgi-bin2/ /var/www/cgi-bin2/
<Location /cgi-bin2>
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
PerlOptions +GlobalRequest
PerlSendHeader On
PerlOptions +ParseHeaders
Options +ExecCGI
Order allow,deny
Allow from all
</Location>
(iii) Create a file named with myajax.pl with the following code:
#!/usr/bin/perl -w
use strict;
use CGI;
use lib '/usr/lib/perl5/site_perl/5.8.8/';
use Apache2::MyAjaxApp;
my $r =shift;
Apache2::MyAjaxApp->handler($r);
(iv) Create a file named MyAjaxApp.pm in following location:
/usr/lib/perl5/site_perl/5.8.8/Apache2/
Put the following code into the file
package Apache2::MyAjaxApp;
use Apache2::Ajax;
use mod_perl2;
sub check {
my $arg = shift;
return ( $arg . " with some extra" );
}
sub Show_Form_sub {
my $html = "";
$html .= "<HTML>";
$html .= "<HEAD>";
$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
return $html;
}
sub Show_Form_sub1 {
my $html = "";
$html .= "<HTML>";
$html .= "<HEAD>";
$html .= <<EOT;
</HEAD>
<BODY>
Invalid
</BODY>
</HTML>
EOT
return $html;
}
sub handler {
my ($class,$r) = @_;
my $ajax = Apache2::Ajax->new($r, 'tester' => \&check);
$r->print($ajax->build_html('html' => \&Show_Form_sub));
return Apache2::Const::OK;
}
1;
Restart your apache service and run in the browser with the following url:
http://localhost/cgi-bin2/myajax.pl
In the previous post, no. 24, i tried to show how to use CGI::Ajax with perl code. Now i am going to use Apache2::Ajax which is mod_perl interface to CGI::Ajax.
This module require mod_perl2, CGI::Ajax, as well as a CGI.pm-compatible CGI module for supplying the param() and header() methods. If available, CGI::Apache2::Wrapper will be used, which is a minimal module that uses methods of mod_perl2 and Apache2::Request to provide these methods; if this is not available, CGI (version 2.93 or greater) will be used.
To use this module you have to make some changes in your httpd.conf, which is located at /etc/httpd/conf/.
To make the use of the below example, some steps need to be followed:-
(i) Create a directory cgi-bin2 in /var/www/.
(ii) Following change must be done your httpd.conf :-
PerlModule ModPerl::Registry
Alias /cgi-bin2/ /var/www/cgi-bin2/
<Location /cgi-bin2>
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
PerlOptions +GlobalRequest
PerlSendHeader On
PerlOptions +ParseHeaders
Options +ExecCGI
Order allow,deny
Allow from all
</Location>
(iii) Create a file named with myajax.pl with the following code:
#!/usr/bin/perl -w
use strict;
use CGI;
use lib '/usr/lib/perl5/site_perl/5.8.8/';
use Apache2::MyAjaxApp;
my $r =shift;
Apache2::MyAjaxApp->handler($r);
(iv) Create a file named MyAjaxApp.pm in following location:
/usr/lib/perl5/site_perl/5.8.8/Apache2/
Put the following code into the file
package Apache2::MyAjaxApp;
use Apache2::Ajax;
use mod_perl2;
sub check {
my $arg = shift;
return ( $arg . " with some extra" );
}
sub Show_Form_sub {
my $html = "";
$html .= "<HTML>";
$html .= "<HEAD>";
$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
return $html;
}
sub Show_Form_sub1 {
my $html = "";
$html .= "<HTML>";
$html .= "<HEAD>";
$html .= <<EOT;
</HEAD>
<BODY>
Invalid
</BODY>
</HTML>
EOT
return $html;
}
sub handler {
my ($class,$r) = @_;
my $ajax = Apache2::Ajax->new($r, 'tester' => \&check);
$r->print($ajax->build_html('html' => \&Show_Form_sub));
return Apache2::Const::OK;
}
1;
Restart your apache service and run in the browser with the following url:
http://localhost/cgi-bin2/myajax.pl
I assume you install Apache2::Ajax for perl5.8. Do you know how to install it for perl5.10? You can't install it using ppm.
ReplyDeleteI guess I could go back to perl5.8 and then install Apache2::Ajax with ppm. But, I'm a little worried about what affect going backwards will have on other perl code of mine.