I am writing a simple dice roller and I would like to pass three form fields to a Perl script, crunch them, and return the result to the same page. I have everything working except the same page requirement.
Is this possible?
Sure. Here is skeleton code with one form field.
use strict;
use warnings;
use CGI;
my $cgi = CGI->new;
my $my_param = $cgi->param('my_param');
print $cgi->header();
print $cgi->html_start();
if (param_valid($my_param)) {
# process params and show result
do_something($my_param);
} else {
# display the form
show_form($my_param);
}
print $cgi->html_end();
Note that when showing (possibly incomplete) form it is generally good idea to include already given fields so that user does not have to re-type them.
Make an AJAX call (in JS) to your Perl program.
Have your Perl program return the results nicely (eg JSON).
Make your AJAX handler make changes to the page based on the JSON data returned.
If you drop the "same page" requirement, then the previous answers will work (note that those answers are NOT the "same page", they just look like the same page). This is most likely the easiest way to accomplish your goal, but to use it you must drop the "same page" requirement.