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

On our intranet we maintain a web site that allows our business partners to administer part of a server. The cgi is in Perl and the form is HTML and Javascript. One particular page allows the group to set up access accounts for users. Works pretty well. The problem is this group wants a pre-populated password field. I found a perl module that suits our needs - Crypt::GeneratePassword. How do I get the generated password into the form and allow the user to accept as is or overwrite with their own? Any suggestions would be appreciated.

Replies are listed 'Best First'.
Re: Pre-Populating an HTML form
by kwaping (Priest) on Jun 01, 2006 at 21:15 UTC
    If it's a straight, unprocessed HTML form, then you're going to have problems. At the very least, you'll need to read the HTML file into a Perl script and use the Perl to insert the $generated_password into the value of the password input. The Perl script would then print an HTML header and print the resulting modified HTML out to the web browser. So instead of loading up http://intranet.yourcompany.com/createuser.html it would be http://intranet.yourcompany.com/createuser.cgi.

    If this is unclear, please say so and I will do my best to clarify (or someone else will).

    ---
    It's all fine and dandy until someone has to look at the code.
      Thanks Kwaping, Following your scheme instead of referencing the Create.html like so: <P><A HREF="Admin_fr_create.htm" NAME="fr_body"><B>Create a New Account</B></A></P> I'd do something like create an "Admin_create.cgi" and encapsulate the Javascript and HTML within? I've been looking at PHP, Server Side Includes, Embed Perl and I'm a little overwhelmed. Using your advise, is there an example in the CGI Programming with Perl, the Camel or Cookbook?
        Here's a very small example of the method I described.
        #!/usr/bin/perl use strict; use warnings; use CGI (); my $cgi = CGI->new(); my $infile = '/path/to/file.html'; open my ($fh), '<', $infile or die "Couldn't open $infile! $!"; read $fh, my ($html), -s $infile or die "Couldn't read $infile! $!"; close $fh or die "Couldn't close $infile! $!"; my $generated_password = function_to_generate_password(); $html =~ s/some_special_target_in_your_html_file/$generated_password/s +gi; print $cgi->header(); print $html;
        If you have any questions, don't hesitate to ask!

        ---
        It's all fine and dandy until someone has to look at the code.
Re: Pre-Populating an HTML form
by CountZero (Bishop) on Jun 01, 2006 at 20:49 UTC
    Just generate the password, store it in a scalar variable and put it in the textfield in the form.

    print '<input type="textfield" value="' . $my_password . '" name="passwordfield"'/>

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      It is type "text", not "textfield" (although as "text" is the default, browsers will tend to be able to error correct).

      In Appendix C XHTML you need a space before the "/", but you've added the end of the tag outside of the string anyway.

      print qq{<input type="text" value="$my_password" name="passwordfield" />};

      I'd suggest sticking to HTML 4.01 (which has much better browser support), and using a templating language rather than printing out from the Perl script directly.

        You are of course entirely right on all accounts. I have to make a note to myself not to answer questions before having had my second cup of strong tea (obviously one cup is not enough anymore).

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Thanks CountZero. What's tripping me up is how to preload the generated password into the form field.

        Well, it all depends on what you have.

        for instance -- if you have SSI (server side includes), you can just do something like:

        <input name='password' value='<!--#include virtual='/cgi/random_password' -->'>

        Another option is to use the whole 'AJAX' concept -- JavaScript to make a call back to the server to ask for a new password, and place it into the field. (of course, then you have a javascript dependancy, and you might as well just do the randomization in JavaScript

        Or, there's the bit that's already been suggested, and just have a CGI to spit out the form. I typically go with this approach, and use the same CGI that generates the backend and initial form, so that I can do data validation, and then re-populate the form, marking any problems.

        Sorry, I misunderstood your question.

        You will have to write a (CGI-)script that dynamically generates the webpage on which is your form. CGI.pm is the archetypical module to do this, but there are many other solutions. Ovid's CGI-course (esp. lesson four and lesson six] will give you a lot of valuable pointers. Personally I tend to use now a templating solution (Template Toolkit) for anything but the most trivial applications, but YMMV.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law