Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: CGI HTML Form Data

by haukex (Bishop)
on Apr 20, 2021 at 05:30 UTC ( #11131500=note: print w/replies, xml ) Need Help??

in reply to CGI HTML Form Data

Do you mean something like this?

#!/usr/bin/perl use Mojolicious::Lite -signatures; use File::Replace qw/replace3/; use Mojo::JSON qw/encode_json decode_json/; use Mojo::File; my $DATA_FILE = Mojo::File->new('/path/to/data.json'); helper defaults => sub ($c, $param) { return undef unless -e $DATA_FILE; my $defaults = decode_json( $DATA_FILE->slurp ); return $defaults->{$param}; }; get '/' => sub ($c) { $c->render(template => 'index') } => 'index'; post '/' => sub ($c) { my (undef,$outfh,$repl) = replace3($DATA_FILE); print $outfh encode_json { name => $c->param('name'), }; $repl->finish; $c->render(template => 'index', message => "Form submitted!"); } => 'submit'; app->start; __DATA__ @@ index.html.ep <!DOCTYPE html> <html> <head><title>Hello, World!</title></head> <body> % if ( stash 'message' ) { <p><%= stash 'message' %></p> % } %= form_for submit => ( method=>'post' ) => begin <div> %= label_for name => 'Your Name' %= text_field name => defaults 'name' </div> <div> %= submit_button 'Submit' </div> %= end <p> %= link_to Reset => 'index' </p> </body> </html>

I'm using my module File::Replace to prevent* problems when there are multiple readers, though if there are multiple writers you'll need to use some kind of file locking or similar method to ensure concurrent writes don't step on each other. Or, use a database!

In the past, the same could be achieved with CGI with its CGI::HTML::Functions. However, since those aren't a best practice anymore, I would not recommend it!

#!/usr/bin/perl use warnings; use strict; use CGI qw/:standard/; use File::Replace qw/replace3/; use JSON::PP qw/encode_json decode_json/; # NOT RECOMMENDED, use an alternative like Mojo instead! my $DATA_FILE = '/path/to/data.json'; my $defaults = decode_json( do { open my $fh, '<', $DATA_FILE or die "$DATA_FILE: $!"; local $/; <$fh> } ); print header; print start_html('Hello, World!'); if ( param('Submit') ) { my (undef,$outfh,$repl) = replace3($DATA_FILE); print $outfh encode_json { name => scalar param('name'), }; $repl->finish; print p( "Form submitted!" ); } print start_form; print div( label({-for=>'name'}, "Your Name"), textfield('name', $defaults->{name}) ); print div( submit('Submit') ); print end_form; print p( a({-href=>url(-absolute=>1)}, 'Reset') ); print end_html;

Update just in case I made it sound too much like these are the only options: of course there are others, see e.g. here, here, and hippos's post here. Also:

* If atomic renames are supported by the OS and FS. If not, some other technique will be necessary. /Update

Unfortunately people here think every question is stupid, and no longer wish to help other people like they used to do. They only want to downvote questions from people trying to learn and ask for help when something is confusing, instead of trying to help them.

It's a FAQ: Why did I get downvoted?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131500]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2022-01-18 10:00 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (52 votes). Check out past polls.