www.webdeveloper.com
Results 1 to 6 of 6

Thread: Perl & Forms

  1. #1
    Join Date
    Sep 2006
    Location
    Ipswich
    Posts
    88

    Perl & Forms

    Hi,

    I am creating a landing page for a website, which is basically a login page.

    The page needs a user to select 1 of 3 criteria (the last of the 3 being the most important) and type in a username and password. So for example the criteria are:

    Region
    District
    Unit

    What I want to know is, can Perl possibly be used to populate a html form select list as the page loads, or can Perl responses only occur once a form has been submitted?

    If I am looking at the wrong technique here, can anyone advice of a better method?

  2. #2
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    393
    Where and when do you get the values that should be populated to the fields?

    If they're always the same, then you just need HTML (no need for server scripting (e.g. Perl)).

    If they're changing and the server knows them when the page with the form is being requested, then it's a job for Perl.

    If you know them after the page is there (e.g. dependent on user input), then we'll talk more :-)

    HTH

  3. #3
    Join Date
    Sep 2006
    Location
    Ipswich
    Posts
    88
    Hi Sixtease,

    Well, the landing page will get the data from a MySQL database, I plan to use 1 database to hold to data relevant to Regions and Distrcts, plus a database for each specific Unit (Units will have a foriegn key to Districts, and Districts the same for Regions).

    It is unlikely that the data for Regions and Districts will change much or often, and yes I agree I could add those in HTML as a manually created select list. However, to save me having to add data twice each time there is a change, I thought it would be neat to have the page just get all the information it needs from the database(s), and that way avoid any differences in the data (between the select list and the database to which the select list data relates).

    I think the "problem" is that this is just a basic HTML mage with a form to submit on it, I've used Perl to do that before (for things like contact forms), but what I don't know is if when the page loads initially, that the data for the select lists I mentioned can be pre-loaded for a form which will then be submitted.

    I guess the most important part of the login page is that I am able to query any usernames and passwords entred against the Unit (District and Region) selected to authenticate users. I guess part of that process is interactive to, because selecting a Region should narrow down the choice of Districts, and selecting a District will narrow down the choice of Units a user can select before entering a username and password.

    Thanks.

    Quote Originally Posted by Sixtease View Post
    Where and when do you get the values that should be populated to the fields?

    If they're always the same, then you just need HTML (no need for server scripting (e.g. Perl)).

    If they're changing and the server knows them when the page with the form is being requested, then it's a job for Perl.

    If you know them after the page is there (e.g. dependent on user input), then we'll talk more :-)

    HTH
    Last edited by A_Tame_Lion; 03-09-2011 at 06:14 AM. Reason: Expanded on functionality of the page.

  4. #4
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    393
    Oh yes, then Perl is your friend. If the page is static HTML and you only need to change it every now and then, then I'd pre-generate the page with Perl and use it as static HTML then.

    It could go along these lines:

    Code:
    # generate-landing-page.pl:
    use strict;
    use warnings;
    use Template;
    use DBI;
    
    $dbh = DBI->connect('dbi:MySQL:dbname=mydb');
    
    my $rows = $dbh->selectall_arrayref('SELECT * FROM districts');
    
    my $tt = Template->new;
    $tt->process('landing_page.tt', {rows => $rows}, 'landing_page.html');
    Code:
    # landing_page.tt
    <html>
      <head>
        <title>welcome</title>
      </head>
      <body>
        <form action="/register/">
          <select name="district">
          [&#37; FOR row IN rows %]
            <option value="[% row.0 %]">[% row.1 %]</option>
          [% END %]
          </select>
          <input type="submit" />
        </form>
      </body>
    </html>
    You'd need to adapt it to your database configuration and table structure, and also rewrite the HTML to match your page. Then you'd generate landing_page.html by executing
    Code:
    perl generate.pl
    and then upload landing_page.html to the server.

  5. #5
    Join Date
    Sep 2006
    Location
    Ipswich
    Posts
    88
    Hi,

    Thanks for the code examples. My understanding is that you are suggesting the page is generated by Perl once, and I then upload it?

    I am familiar with Perl Pragma and dbi statements, but the last 2 lines of your first block of code are a little new to me (althouhg it looks like it is creating a variable tt to hold the process that outputs a new landing page?):

    Code:
    my $tt = Template->new;
    $tt->process('landing_page.tt', {rows => $rows}, 'landing_page.html');
    As is the following statement (which looks like is is a way of feeding input into the select list?):

    Code:
          <select name="district">
          [&#37; FOR row IN rows %]
            <option value="[% row.0 %]">[% row.1 %]</option>
          [% END %]
          </select>
    I'm going to take a little while to study the code and reflect on how I need to modify it to reflect my database configuration and table structure.

    Many thanks.

  6. #6
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    393
    Yes, I meant it exactly as you say. Perl would generate it once and then you'd upload it. This way, you need not manually write it and the server doesn't need to bother fetching the same data on every request.

    You also understood the concept behind the mentioned code blocks correctly. $tt is a Template object. Its process method takes the name of a template and expands things in &#91;% ... %&#93; tags. The second block of code is the template itself. It uses the template-toolkit language.

    To get a better grip of it, consult the Template Toolkit documentation:

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles