Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

perl form

by britney (Acolyte)
on Oct 09, 2003 at 20:55 UTC ( [id://298103]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

QUESTION: How do you write the script so if someone enter serail number in fromcgi.pl and the otherfields, but after he submit, it will get part number from getpart.pl and write to default.db file Date|Serialnumber|Partnumber|Quantity|Extra
This will auto fill in partnumber (hidden field).

Code follows:

1. oneline form: formcgi.pl
#!/usr/bin/perl print "Content-type:text/html\n\n"; print <<EndOfHTML; <HTML> <HEAD> <TITLE>Test</TITLE> </HEAD> <BODY bgcolor=ffffff> <BR><center><h1><Font color=red>Check How it work</font></h1></center> <BR><center> <FORM name="submitform" action="http://testit.com/cgi-bin/addrecord.pl +" method="post" > <TABLE WIDTH=614 BORDER=1 CELLPADDING=4 CELLSPACING=3 STYLE="page-brea +k-before: always"> <COL WIDTH=187> <COL WIDTH=400> <TR VALIGN=TOP> <TD WIDTH=187> <P>Serial Number:</P> </TD> <TD WIDTH=400> <INPUT type="text" name="serialnumber" size="20"> </TD> </TR> <TR VALIGN=TOP> <TD WIDTH=187> <P>Part Number:</P> </TD> <TD WIDTH=400> <INPUT type="text" name="partnumber" size="20"> </TD> </TR> <TR VALIGN=TOP> <TD WIDTH=187> <P>Quantity:</P> </TD> <TD WIDTH=400> <INPUT type="text" name="quantity" size="20"> </TD> </TR> <TR VALIGN=TOP> <TD WIDTH=187> <P>Extra Opt:</P> </TD> <TD WIDTH=400> &nbsp;<SELECT NAME="DIMMOpt"> <OPTION SELECTED VALUE="null">Select Opt</OPTION> <OPTION>N/A</OPTION> <OPTION>Full POP</OPTION> <OPTION>Haft POP</OPTION> </SELECT> </TD> </TR> </TABLE> <P><INPUT type="submit" value="Run"><INPUT type="reset" value="Oops! S +tart again"></P> </FORM></center> EndOfHTML print "</BODY> </HTML>\n";
2. addrecord.pl
#!/usr/bin/perl print "Content-type:text/html\n\n"; use Time::Local; $today = timelocal(localtime); $now = &unix_to_date($today); sub unix_to_date { # -------------------------------------------------------- # Returns the date in the format "mm-dd-yyyy". # Warning: If you change the default format, you must also modify + the &date_to_unix # subroutine below which converts your date format into a unix ti +me in seconds for sorting purposes. my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $dayl +ight) = localtime(time()); ($day < 10) and ($day = "0$day"); $mon += 1; ($mon < 10) and ($mon = "0$mon"); $year += 1900; return "$mon\/$day\/$year"; } $datafile = "default.db"; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/~!/ ~!/g; $FORM{$name} = $value; } open(OUTF,">>default.db") or dienice("Couldn't open defaul +t.db for writing: $!"); # This locks the file so no other CGI can write to it at the # same time... flock(OUTF,2); # Reset the file pointer to the end of the file, in case # someone wrote to it while we waited for the lock... seek(OUTF,0,2); print OUTF "$now|$FORM{'serialnumber'}|$FORM{'partnumber'} +|$FORM{'quantity'}|$FORM{'DIMMOpt'}|\n"; close(OUTF); print <<EndHTML; <html><head><title>New record has been added !</title> <meta http-equiv="refresh" content="2; url=http://testit.com/cgi-bin/f +ormcgi.pl"> </head> <body> <center><h2>New record has been added today $now !</h2> <BR>Thanks !! </center> </body></html> EndHTML sub dienice { my($msg) = @_; print "<h2>Error</h2>\n"; print $msg; exit; } 3. getpart.pl This is a perl script from someone, it take serial number and give me +part number and it pulled from a website. <code> Example: $getpart 1234ABCD I will get the result is $501-1234-01
and here is the code:
#!/usr/bin/perl -w $numArgs = $#ARGV + 1; if ($numArgs == 1) { $INPUT = "$ARGV[0]"; } else { exit; } use LWP::Simple; $page = get("http://from/this/SfcStatus?sfcnumber=${INPUT}&tested=DONT +CARE"); #print $page ($plain_text = $page) =~ s/<[^>]*>//gs; @item=split(' ',$plain_text); #$count = 0; #foreach $i (@item) { # print "$count : $i\n"; # $count += 1; # } #exit; # First, find Shop Order, MFG Center, and Status foreach $i ( 30...60) { # print " $i $item[$i]\n"; if ( $item[$i] eq 'ORDER' ) { $SHOP = $item[ $i + 1 ]; if ( length $SHOP < 8 ) { $SHOP .= "\t"; } } elsif ( $item[$i] eq 'CENTER' ) { $MFG_CENTER = $item[ $i + 1 ]; if ( length $MFG_CENTER < 8 ) { $MFG_CENTER .= "\t"; } } elsif ( $item[$i] eq 'GROUP' ) { $ITEMGROUP = $item[ $i + 1 ]; } elsif ( $item[$i] eq 'STATUS' ) { $STATUS = $item[ $i + 1 ]; if ( length $STATUS < 8 ) { $STATUS .= "\t"; } last; } } # KEY: Part Number Serial Number Shop Order MFG CENTER STA +TUS if ( $item[60] eq 'DEKIT' ) { print "$item[34]\n"; } elsif ( $item[27] eq 'SHIP' && $item[60] ne 'DEKIT' ) { print "$item[34]\n"; } else { print "$item[26]\n"; }
Thanks

Edited by castaway - added readmore and moved question to top.

Replies are listed 'Best First'.
Re: perl form
by kutsu (Priest) on Oct 09, 2003 at 21:28 UTC

    That's alot of work, but here are a few modules/tuturials on cgi and templating that might make your work easier

    1. Ovid's incredable cgi course
    2. Template Toolkit a templating system some prefer over cgi or to use with cgi
    3. HTML::Template another Templating system some prefer
    4. DBI, If you have to deal with a database you'll need this

    Hope this points you in the right direction

    "Pain is weakness leaving the body, I find myself in pain everyday" -me

Re: perl form
by benn (Vicar) on Oct 10, 2003 at 12:41 UTC
    I think you'd be better reorganising this. Formcgi.pl doesn't need to exist at all - this could simply be a static HTML form, as all it does is pass parameters, with no dynamic generation necessary. Then you could simply add the code in "getpart.pl" into "addrecord.pl" (as a GetPartNumber subroutine or something similar), return the value and write out to the db in one fell swoop.

    As stated above though, CGI.pm would be of *great* help to you here, especially in the parsing of the parameters.

    Cheers, Ben.

      Hi Benn, Yes , I tried to use form in html first, but I was stuck when it call for getpart.pl Would you mind to show me to to write this addrecord.pl and have sub GetpartNumber I tried and get no luck. Thanks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2025-03-24 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (63 votes). Check out past polls.