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

CGI HTML Form Data

by johnfl68 (Scribe)
on Apr 19, 2021 at 21:08 UTC ( [id://11131481]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I have a form that I need to pre-populate with the previous responses made from that form. I was going to pre-populate the HTML Form with JSON data using jquery and then pass to a CGI to save the results to a JSON file, but Chrome is now blocking external files like this, even though everything is on the same local server.

So now I am doing this with CGI to load the JSON values and output the HTML with the form data pre-populated.

So can I submit the form data to the same CGI Perl script? Or do I need to have one CGI for the HTML Form, and another CGI to process the form and save to the JSON file?

I see lots of examples of posting form data from HTML to a CGI to process, and I have done this before. I am not finding any examples of how to bring the data into the same CGI script from the outputted HTML.

I am probably missing something simple, but if the only way to do it is with 2 CGI scripts, that will work as well.

Thank you.

Original content restored above by GrandFather

PerlMonks admins, please remove. 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.

I will look for help elsewhere.

Thank you.

Replies are listed 'Best First'.
Re: CGI HTML Form Data
by hippo (Bishop) on Apr 20, 2021 at 08:04 UTC
    So can I submit the form data to the same CGI Perl script? Or do I need to have one CGI for the HTML Form, and another CGI to process the form and save to the JSON file?

    You can submit the form data to the same CGI Perl script. It is quite a common pattern for one script to act as both form generator and form handler for precisely this reason. There are also a whole bunch of tools on CPAN to help you accommodate this. I have used a combination of WWW::Form and WWW::FieldValidator to good effect but there are plenty of others too.


    🦛

Re: CGI HTML Form Data
by haukex (Archbishop) on Apr 20, 2021 at 05:30 UTC

    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!

    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?

Re: CGI HTML Form Data
by Fletch (Bishop) on Apr 20, 2021 at 04:04 UTC

    I'm sorry you're miffed at your deficit of fake internet points, but your question really isn't very good.

    • There's nothing specifically about Perl in it to start with.
    • There's vague mention of "Chrome is now blocking external files" from a "local server", but I'm not sure what that means. Are you trying to do something that's partially reading from local disk files on the browser machine? If everything's coming from the same origin, what / why is blocked? But if this is coming from local disk files then how would a CGI invocation get into the mix?
    • Additionally there's no actual code to gnaw on or have an idea
    • There's not really a sample of what the frak you're actually trying to do. Not even an abstract "On page A I have a form with field 'FOO'. I need to take the value from 'FOO' and do X to it then post it to page B".

    This is a jumble about forms and data and posts and CGIs and . . . I just don't understand any of what you're actually trying to accomplish. Why are you trying to post something to an intermediary destination only to re-post it somewhere else? Why can you not just post to the final destination directly? If there's something about the data that needs to be massaged, you seem to have access to the form with jQuery/JS so . . . massage it before it's posted.

    Edit: In comparison to another of your recent questions, Regex to Array lookup question, can you see the deficit deficiency this against that? There while you were a bit vague and still didn't provide any, you know, code you did at least provide concrete sample data (your URLs, a handwavy list hinting at what you were wanting to pull out from the URLs, an example what you wanted to use that data for) and people were able to have a bit to work with. This . . . /shrug

    Edit2: A phrase.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: CGI HTML Form Data
by Anonymous Monk on Apr 19, 2021 at 23:24 UTC
      >What? confused. If issue with chrome diagnose/repair, not write new program.

      bloody'ell. Skynet confirmed.

Re: Please remove
by perlfan (Vicar) on Apr 20, 2021 at 01:43 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11131481]
Approved by philipbailey
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found