Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

dynamically built form getting data parameters

by golroch (Initiate)
on Jun 10, 2016 at 13:38 UTC ( [id://1165286]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to figure out how to get all the parameters from a form that is dynamically built based on data retrieved from the server. I cannot update perl (5.8) due to vendor limitations. The web page has a simple set of data that is read from a csv file and my script creates a webpage form that asks the professor to check which books they want to keep. The data is sent via a post method to a perl script that grabs the data, creates an email and sends it via blat. Pretty simple until I realized the following. The problem is some professors may have more than one book (maybe 50) on reserve. I looked at the Data:Dumper module and realized I could just increment the value/name for each row of data and check to see if it is undefined. Is this a decent way? I don't have access to an sql database to make this easier since it is used by the vendor and creating more tables could break something. Not a perl expert, but seeking the wisdom here and hope to steadily improve. Thanks in advance
<input type="hidden" value="Doe, Jane" name="Professor"> <input type="hidden" value="0116411160645 " name="IDNum"> <input type="hidden" value="Perl for dummies" name="Title"> <input type="hidden" value="COIS 101 name="Course"> <input type="hidden" value="13" name="Charges"> <input type="hidden" value="09-12-2013" name="LastUsed"> <input type="hidden" value="11-27-2012 " name="Placed"> <tr> <td><input type="checkbox" value="KEEP THIS ITEM" name="Status"></td> <td>0116411164951 </td> <td>PF 3112 .W5 2013 </td> <td>Perl for dummies </td> <td>COIS101 </td> <td>13 </td> <td>09-12-2013</td> <td>11-27-2012 </td> </tr> <input type="hidden" value="KEEP THIS ITEM"" name="Keep1"> <input type="hidden" value="Smith, Josh" name="Professor1"> <input type="hidden" value="0116411162016 " name="IDNum1"> <input type="hidden" value="Modern Perl" name="Title1"> <input type="hidden" value="COIS102" name="Course1"> <input type="hidden" value="999" name="Charges1"> <input type="hidden" value="09-12-2016" name="LastUsed1"> <input type="hidden" value="11-27-2016 " name="Placed1"> <tr> <td><input type="checkbox" value="KEEP THIS ITEM" name="Status"></td> <td>0116411162016 </td> <td>Modern Perl </td> <td>COIS102</td> <td>999 </td> <td>09-12-2016</td> <td>11-27-2016 </td> </tr>

Replies are listed 'Best First'.
Re: dynamically built form getting data parameters
by hippo (Bishop) on Jun 10, 2016 at 13:53 UTC
    I could just increment the value/name for each row of data and check to see if it is undefined. Is this a decent way?

    It's not a terrible way but it would mean you need to do the same with every repeating parameter including "Status" which you have not done above.

    It may be preferable to keep the same name without the increment and retrieve them as arrays or arrayrefs. That would probably be my approach but yours is workable too.

      Yes, that is exactly what I am attempting to do. Dealing with an array is what I am familiar with though I should read up on array references. How would I build an array of the data? Thanks for the path to wisdom hippo.
        The problem with multiple values in an array is that a checkbox field will not exist in the values sent by the form unless it is checked. In your example, if you kept the names the same and only one checkbox was ticked it would come in like this:
        Professor => ["Doe, Jane","Smith, Josh"], IDNum => ["0116411160645","0116411162016"], Status => "KEEP THIS ITEM",
        You have no way of telling which item this refers to. So you will need to group each item somehow, and I think your idea of incrementing is fine, as long as you do it to all parameters as hippo points out.
        You might find this helpful..Re: Pushing rows of data into array. Some examples of Array of Array and how to access them.

        Hope that helps.

        Also, you may find DBD::CSV of use. It is possible access a CSV file with SQL statements. Doesn't require and DB admin as the "database" is just a CSV file.

        OK, let's suppose you use CGI::Lite. Here's a simplistic script you might write to process any number of batches of fields:

        #!/usr/bin/perl -T use strict; use warnings; use CGI::Lite (); print "Content-type: text/plain\n\n"; my $cgi = CGI::Lite->new (); my $params = $cgi->parse_form_data; my $i = 0; while ($i <= $#{$params->{Professor}}) { print "Professor $i is $params->{Professor}->[$i]\n"; print "IDNum $i is $params->{IDNum}->[$i]\n"; # ... carry on with other fields here $i++; }

        Update: See also the get_multiple_values convenience method in the documentation which is a benefit if you don't know for sure that there will be more than one of each item.

Re: dynamically built form getting data parameters
by Anonymous Monk on Jun 11, 2016 at 00:16 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found