3

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?

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I would make it output HTML form with all known fields set according to input received by the script, hence $my_param is given as a parameter. You may also want to include some error/help text in case field validation fails.
3

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.

1 Comment

Google for "perl ajax example"
3

Yes. Simplified: If the parameters are present, do the processing, if not, display the page with the fields.

 if ($q->param('field1')) {
    # Process params
 } else {
    # display fields in a form
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.