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

I seem to be missing something !

Simple scenario: I want to save the details of a validated form to a database. Up to now, I have been using the tried and tested procedure:

What I would like and *know* must be a common process is to be able to avoid having to {send the data back to client} so that it would look more like :

There is an underlying assumption here that I have control over the local scripting language (javascript).

Any pointers ? which "M" I should FR (as in RTFM) ?

Update

Thanks for all responses to date.

It is clear that my original post was not worded correctly.

I think the error was in the way I said:

generate new (r/o) page/form with data to show data is saved.

What I should have said was that I wanted to reuse the data/page at the client to avoid having to resend data from the server to generate a new page

The solution I have come up with is to

If the simplicity of the solution makes my original post seem trivial I apologise.

Replies are listed 'Best First'.
Re: HTML Form Submission & Save to MySQL
by borisz (Canon) on Jan 19, 2005 at 11:32 UTC
    Use a cookie and a session.
    form->{send_data_to_server} -> invoke pl to create/write a session, add a cookie to the headers, send back the new page.
    No need for javascript at all.
    Boris
      borisz, as per my update, I have gone a different route, but would like to say thanks for pointing me in the direction of sessions (new to me). Looks like a whole new world of "data persistence" ..... Cheers.
Re: HTML Form Submission & Save to MySQL
by drfrog (Deacon) on Jan 20, 2005 at 02:43 UTC
    something like this:
    #!/usr/bin/perl use strict; use CGI; use HTML::Template; use DBI; my $q = CGI->new; my $dbh = DBI->connect( 'dbi:Pg(RaiseError=>1,Taint=>1):dbname=write;host=loca +lhost', 'user', 'password', ); if ( $q->param('post') eq 'post' ) { #post info to db my $sth_get_text = $dbh->prepare( q{ insert into write (name,text) values (?,?) } ); my $insert = $sth_get_text->execute( $q->param('name'), $q->param( +'text') ); $sth_get_text->finish; $q->redirect('/perl/write.pl'); } else { #display my $sth_get_text = $dbh->prepare( q{ select text,name from write } ); $sth_get_text->execute(); my $text = $sth_get_text->fetchall_arrayref; $sth_get_text->finish; my %hash; my $stuff; foreach my $text_in ( @{$text} ) { push @{ $hash{stuff} }, { text => $text_in->[0], name => $text_in->[1], }; } my $template = HTML::Template->new( filename => 'write.tmpl', die_on_bad_params => 0 ); $template->param(%hash); print $template->output; } $dbh->disconnect;
    note that this one also contains a html template loop for extracting the tuples out of DB and displaying them, along with a form

    write.tmpl is a template which contains the templater loop info and of course a form which submits to this script with the name and text fields present

Re: HTML Form Submission & Save to MySQL
by Miguel (Friar) on Jan 19, 2005 at 17:05 UTC
    For short:
    #!/usr/bin/perl -w use strict; use CGI; my $q = new CGI; print $q->header, $q->start_html; if ( $q->param("name") ) { # Validate data and insert it into DB Answer(); } else { Form(); } print $q->end_html; sub Form { print $q->start_form, "Your name: ", $q->textfield("name"), " ", $q->submit, $q->end_form; } sub Answer { print "Your name is ", $q->strong($q->param("name")) }
    Use CGI

    This way you validate data AFTER printing out the document header. If you want to validate data BEFORE you need to change the way you call those functions. You cannot print anything to the browser before printing the document header.

    UPDATE: OOPSSS! I misunderstood your question! I'm sorry. I thought you were passing the form via CGI. But that's not the case.

    If you're using somthing like: file.HTML(form)->file.cgi(save data, send a cookie)->file.HTML(answer with data) you might need javascript to read the cookie in the 3rd step.