|1 Standard CGI::Ajax example||
Start by defining a perl subroutine that you want available from
whether or not an input is odd, even, or not a number (NaN):
Alternatively, we could have used coderefs to associate an exported name...
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)...
The exported Perl subrouting is triggered using the OnKeyUp event handler of the input HTML element. The subroutine takes one value from the form, the input element <B>val1B>, and returns the the result to an HTML div element with an id of <B>resultdivB>. 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 isnt mandatory - as will be explained in the <B>AdvancedB> usage.
And if we used a coderef, it would look like this...
Now were ready to print the output page; we send in the cgi object and the HTML-generating function.
CGI::Ajax has support for passing in extra HTML header information to the CGI object. This can be accomplished by adding a third argument to the build_html() call. The argument needs to be a hashref containing Key=>value pairs that CGI objects understand:
Thats it for the CGI::Ajax standard method. Lets look at something more advanced.
|2 Advanced CGI::Ajax example||
Lets say we wanted to have a perl subroutine process multiple
values from the HTML page, and similarly return multiple values
back to distinct divs on the page. This is easy to do, and
requires no changes to the perl code - you just create it as you
would any perl subroutine that works with multiple input values
and returns multiple values. The significant change happens in
process the returned information from your Perl subroutine.
This is possible, and the only requierment is that you change
your event handler code...
|4 URL/Outside Script CGI::Ajax example||
There are times when you may want a different script to
return content to your page. This could be because you have
an existing script already written to perform a particular
task, or you want to distribute a part of your application to another
script. This can be accomplished in CGI::Ajax by using a URL in
place of a locally-defined Perl subroutine. In this usage,
you alter you creation of the CGI::Ajax object to link an
a coderef or a subroutine.
This will work as before in terms of how it is called from you event handler:
This is good, but what if you need to send in arguments to the other script which are directly from the calling Perl script, i.e. you want a calling Perl scripts variable to be sent, not the value from an HTML element on the page? This is possible using the following syntax:
In both of the above examples, the result from the external script would get placed into the resultdiv element on our (the calling scripts) page.
For example, from your Perl script, you would...
In order to rename parameters, in case the outside script needs specifically-named parameters and not CGI::Ajax args default parameter name, change your event handler associated with an HTML event like this
The URL generated would look like this...
You would then retrieve the input in the outside script with this...
This will get the value of our HTML element with and id of div_id, and submit it to the url attached to myparam__. So if our exported handler referred to a URI called script/scr.pl, and the element on our HTML page called div_id contained the number 42, then the URL would look like this script/scr.pl?myparam=42. The result from this outside URL would get placed back into our HTML page in the element resultdiv. See the example script that comes with the distribution called pjx_url.pl and its associated outside script convert_degrees.pl for a working example.
<B>N.B.B> These examples show the use of outside scripts which are other perl scripts - but you are not limited to Perl! The outside script could just as easily have been PHP or any other CGI script, as long as the return from the other script is just the result, and not addition HTML code (like FORM elements, etc).
Note that all the examples so far have used the following syntax:
There is an optional third argument to a CGI::Ajax exported function that allows change the submit method. The above event could also have been coded like this...
(POST and post are supported)
We have implemented a method to prevent page cacheing from undermining the AJAX methods in a page. If you send in an input argument to a CGI::Ajax-exported function called NO_CACHE, the a special parameter will get attached to the end or your url with a random number in it. This will prevent a browser from caching your request.
The extra param is called pjxrand, and wont interfere with the order of processing for the rest of your parameters.
Also see the CACHE() method of changing the default cache behavior.
Follow any bugs at our homepage....
Check out the news/discussion/bugs lists at our homepage:
Brian C. Thomas Brent Pedersen CPAN ID: BCT firstname.lastname@example.org email@example.com significant contribution by: Peter Gordon <firstname.lastname@example.org> # CGI::Application + scripts Kyraha http://michael.kyraha.com/ # getVal(), multiple forms Jan Franczak <email@example.com> # CACHE support Shibi NS # use ->isa instead of ->can others: RENEEB <RENEEB [...] cpan.org> stefan.scherer RBS Andrew
This module was initiated using the name Perljax, but then registered with CPAN under the WWW group CGI::, and so became CGI::Perljax. Upon further deliberation, we decided to change its name to CGI::Ajax.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
|perl v5.20.3||CGI::AJAX (3)||2008-10-08|