Pop up Confirmation Box

Hi,

I was writing a simple web application using Perl -CGI. When users try to do some operations, I wanted like a pop-up confirmation box. Is this possible with Perl-CGI?

Thanks in advance.

Regards,
garric

The standard approach is to use the window.confirm() DOM Javascript method.

DOM:window.confirm - MDC

Thanks. Looks like this will work for me. But how do I integrate it into a CGI script?

Regards,
garric

Because what CGI.pm does is to simply print output to the STDOUT (this is the protocol of CGI itself), you can always print the needed Javascript from your program.

print <<EOF;
<script type="text/javascript">
function confirmOk() {
    return window.confirm("Confirm deletion?");
}
</script>
EOF

This should be printed somewhere inside the "head" or "body" tags. This prints a Javascript function definition, but just like a Perl subroutine definition, it is not called immediately. It must still be called from somewhere in the program.

If you still wish to use CGI.pm to generate the entire HTML output after reading this post (which I don't really recommend because I think that facade doesn't do much good), CGI.pm exposes some attributes that correspond to standard Javascript event handlers applied to HTML objects that you can use to execute custom Javascript just like the one above. You can find more information at:

CGI - perldoc.perl.org

Then you can do something like this to install the handler:

print button(-name=>'button_name',
			  -value=>'user visible label',
			  -onClick=>"return confirmOK()");

If you think that printing javascript text from your Perl program is very ugly, like I do, use a template system to generate the returned content rather than using the "tree-building" mode of CGI.pm (but do use CGI.pm to generate headers, capture GET/POST/COOKIE params etc. - that's good). My favourite:

Template Toolkit Home Page

I advise you to really find out more about HTML and Javascript or your concepts may not allow you to go too much further, no matter you are going the CGI.pm route, template route or some other approaches. Because Javascript is complex, I suggest you look for resources to learn on your own but definitely don't look into the Perl docs for that!

Thanks for this info. Yes, I think I need to go back and understand Javascript and HTML better.

As for this problem, this is what I wrote (still unsuccessfully)

my $display_form = new CGI;
$JSCRIPT="function confirmOk() { return window.confirm('Confirm deletion?'); }";

print $display_form->header("text/html");
print $display_form->start_html(
                                                -meta=>{'http-equiv'=>'Content-Language','content'=>'en-us'},
                                                -script=>$JSCRIPT);
print $display_form->start_form(-title=>"Operation", -style=>'font-size: 9pt; color:  #000000 ; font-family: Tahoma', -action=>"./operation.pl", -method=>"POST");
print $display_form->submit(-name=>'Operation', -onClick=>"return confirmOK()");

Clicking on the submit button still doesn't result in the pop-up. Do you see anything I am missing here?

Regards,
garric

Oh, sorry. Should be

return confirmOk()

Didn't notice the wrong casing. Javascript is case-sensitive.

that worked, thanks :slight_smile: