Hi Monks, I found a tutorial for a simple database CGI script and I am wanting to build on it, so it accepts input via a web form using drop down menus, instead of text boxes.
The tutorial is perfect for what I am trying to make and already includes subroutines for searches and deleting records. I am trying to make a simple change management database via a web browser form, but I do not want to be typing all the information in the text boxes. It says in the tutorial that only text and textarea boxes are supported for the type of field, but I was thinking with a bit of a change I could change that to drop down boxes, or option buttons. Perhaps by having separate hashes for the menu items.

The full tutorial with the whole listing of code is at: Link to tutorial I've included some of the original code snippets and what I have changed them to.
It originally listed these values as a hash of arrays.

# Define Your Database Fields Here Like So # Field Name => [Number, 'Readable Name', 'type of field, two op +tions are text or textarea' #%fields = ( # ID => [0, 'Link ID:', + 'text'], # Name => [1, 'Name:', + 'text'], # Email => [2, 'E-Mail Address:', + 'text'], # Phone => [3, 'Phone Number:', + 'text'], # Address => [4, 'Street Address:', + 'textarea'] #);
I've tried changing it to:
%fields = ( ID => [0, 'Link ID:', + 'text'], change_type => [1, 'Type Of Change:', + 'drop'], change_summary => [2, 'Summary Of Change :', + 'text'], machines => [3, 'Machines involved :', + 'drop'], date => [4, 'Date of change:', + 'text'], time => [5, 'Time of change:', + 'text'], change_reason => [6, 'Reason for change:', + 'textarea'] ); %fields_drop = ( #types of system change 'sys' => 'sys', 'ops' => 'ops', 'cmd' => 'cmd');
The code that builds the form is as follows:
sub build_record_page { my (%record) = @_; my ($val) = ""; my ($html) = qq~<TABLE border=1 bgcolor="#FFFFFF" cellspacing=0 cellpa +dding=4> <TR bgcolor="#C0C0C0"> <TD colspan=2><CENTER><font size=-1> Record </CENTER>< +/TD> </TR>~; foreach $obj (@db_fields) { if ($obj eq $db_key) { next; } $html .= qq~<TR bgcolor="#DDDDDD"><TD><FONT SIZE=-1>$d +b_name{$obj}</TD><TD>~; if ($db_type{$obj} eq "text") { # Makes the text box if ($record{$obj}) { $val = qq~ VALUE="$record +{$obj}"~; } else { $val = ""; } $html .= qq~<INPUT TYPE="text" NAME="$obj" siz +e="20" $val>~; } if ($db_type{$obj} eq "drop") { # Makes the dropdown +box if ($record{$obj}) { $val = qq~ VALUE="$record +{$obj}"~; } else { $val = ""; } $html .= qq~<SELECT NAME="$obj +" <option value=""></option> $val>~; # The bit that I am stuck on I n +eed to populate a drop down box from the fields_drop hash, but that n +eeds to depend on which whether it is the type of change, or the mach +ines to which the change will be applied. } elsif ($db_type{$obj} eq "textarea") { # Makes the tex +tarea if ($record{$obj}) { $val = qq~$record{$obj}~; + } else { $val = ""; } $html .= qq~<TEXTAREA NAME="$obj" ROWS="4" COL +S="40">$val</TEXTAREA>~; } $html .= qq~</TD></TR>~; } $html .= "</TABLE><P>"; print $html; }
Can anybody help please?

In reply to Using drop down boxes in a CGI script by wishartz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.