Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically add data 1

Status
Not open for further replies.

blarneyme

MIS
Jun 22, 2009
160
0
0
US
Below is my code, but I want it to add rows each time if necessary. So when I press submit it would bring up the same form again but display the previously added data to the form. When done, then it would send all 6 (or however many) rows of data.
Code:
use CGI;
$q = new CGI;

print $q->header;
print $q->start_html;

unless($q->param) {
    print $q->start_form('New Request');
    print "<H1>New Request</H1><hr>\n";
    print "<P>Name:", $q->textfield('From', '', 20, 60);
    print "<P>Phone:", $q->textfield('phone', '', 20, 60);
    print "<P>Date:", $q->textfield('today', '', 20,60);
    print "<P>Completion date:", $q->textfield('completion', '', 20, 60);
    print "<PRE>";
    print "<P>To:", $q->popup_menu('Recipient',$RECIP);
    print "<P>Entry type:",$q->popup_menu('Entry',$ENTRY);
    print "<P>Subnet:",$q->textfield('IP', '', 20, 20);
    print "Hostname: ",$q->textfield('Hostname', '', 20, 60);
    print "<P>Domain:", $q->popup_menu('Domain',$DOMAIN);
    print "Comments: ", q->textfield('Comments', '', 20, 60);
    print "</PRE>";
    print "<H2>Mail this request</H2>";
    print "<P>", $q->checkbox_group('Mail_me', $PAGE);
    print "<hr>";
    print "<P>", $q->submit('Submit');
    print $q->end_form;
} else {

    $requester = $q->param('From');
    print "<H1>New Request Complete</H1><hr>\n";

    print "<P>New entries:", $q->param('Entry'), $q->param('IP'),
    $q->param('Hostname'), $q->param('Domain'), $q->param('Comments');

    if ($requester && (@interest = $q->param('Mail_me'))) {
        print "<P>You will receive e-mail whenever the ";
        for (@interest) {
          SWITCH:{
              /DNS/ && do {
                  print "DNS ";
                  last SWITCH;
              };
              /Port/ && do {
                  print "Port ";
                  last SWITCH;
              };
              my $x = 1;
          }
         }
         print "entries need updated.";
    }
    print "<hr>";
    print "<H3>Debug Parameter Dump</H3>", $q->dump, "<hr>" if $DEBUG;
}

print $q->end_html;
 
Hello,

I would use a session to hold a hash data reference. This way I can easily use/manipulate the data structure throughout the period of a session.

For example:

Code:
#! /usr/bin/perl
use strict;
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);
use CGI::Session;

#Create / open session
my $session = new CGI::Session();
print $session->header();

#Read hash data from session.
my $hash = ($session->param(-name=>'Hash')) ? $session->param(-name=>'Hash') : undef;

#Make changes to the hash if a button is pressed
if (param('DeleteRows')) {
	#Loop through each checked X and remove them from the hash
	my @checked = param('X');
	foreach (@checked) {
		delete($hash->{$_});
	}
}
elsif (param('AppendRow')) {
	#Append each form field to the hash
	$hash->{param('RowID')}{'FirstName'} = param('FirstName');
	$hash->{param('RowID')}{'LastName'} = param('LastName');
	$hash->{param('RowID')}{'Age'} = param('Age');
}
elsif (param('Finished')) {
	#End the session, send the data, kill the script
	$session->delete();
	senddata($hash);
	sub senddata { }
	print "<p>Finished - <a href=\"script.pl\">New</a></p>";
	die;
}

#Write hash data to session
$session->param(-name => 'Hash', -value => $hash);
$session->flush();

#Print
print "<form method=\"post\" action=\"script.pl\"><ul>";
my $nextrow = 1;
foreach my $rowid (sort keys %{$hash}) {
	#Calculate next row
	$nextrow = ($nextrow <= $rowid) ? $rowid + 1 : $nextrow;
	print "<li>| X<input type=\"checkbox\" name=\"X\" value=\"$rowid\" /> | $rowid | $hash->{$rowid}{'FirstName'} | $hash->{$rowid}{'LastName'} | $hash->{$rowid}{'Age'} |</li>";
}
print "<p><input type=\"submit\" name=\"DeleteRows\" value=\"Delete Rows\" /></p>";
print "</ul><ul>";
print "<li>Row ID: <input type=\"TEXT\" name=\"RowID\" value=\"$nextrow\" /></li>";
print "<li>First Name: <input type=\"text\" name=\"FirstName\" value=\"\" /></li>";
print "<li>Last Name: <input type=\"text\" name=\"LastName\" value=\"\" /></li>";
print "<li>Age: <input type=\"text\" name=\"Age\" value=\"\" /></li>";
print "<p><input type=\"submit\" name=\"AppendRow\" value=\"Append Row\"></p>";
print "</ul><ul>";
print "<p><input type=\"submit\" name=\"Finished\" value=\"FINISHED...\" /></p>";
print "</ul></form>";

Goodluck,

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top