CyCliC has asked for the wisdom of the Perl Monks concerning the following question:

my code looks something like this

print header( "text/html" ); print start_html(-title => "Bookshelf at Reddo Networks", -bgcolor => "#ffffff" ); print start_form(); print p("Document nr: ", textfield("doc_nr")); print p("Document name: ", textfield("doc_name")); print p("Author: ", popup_menu("author", \@authors)); print p(submit("DO"), reset("clear")); print end_form(), hr(); print_content(); print end_html; write_content(); sub print_content { open(DAT, "<doc_number.dat"); @all = <DAT>; foreach $b (@all){ print p($b); } close(DAT); } sub write_content { print p("tada"); }
how do I run the write_content function when the submit button has been pressed

Replies are listed 'Best First'.
Re: Executing a function on submit
by tadman (Prior) on Jul 30, 2001 at 14:44 UTC
    You can't send the script to the client, unless you can do this all in JavaScript, which in your trivial example you probably can.

    If you're doing something more ambitious, you will have to use a separate script, or one that has a "trigger" in it. Something like:
    my $cgi = new CGI; # Your old stuff # : print end_form(), hr(); print_content(); print_end_html(); if ($cgi->param('DO')) { write_content(); }
    Make sure that the ACTION parameter of the form is set to itself, which it should be if you don't give it any parameters. When you click on the "DO" button, it should send a DO parameter to the server with the value of the button.
Re: Executing a function on submit
by voyager (Friar) on Jul 30, 2001 at 18:58 UTC
    Javascript in the browser is the answer. There are a number of ways, but probably you want an onClick event on the button that does something like document.write("tada")

    Note: there are several ways to detect events that can trigger Javascript, and there are a mulititude of ways to get content written to the browser. Now, if you want to get cross-browser compliant, it gets trickier.