attempting code first perl/cgi script , have run areas don't quite understand how move forward. created html page has information user inputs , sent script. script outputs information on second page user errors should user not follow directions. forms take on appearance following:
- item number: user enters number
- product name:
- product cost:
- selling price:
product category: - radio button 1 - radio button 2 - radio button 3
quantity on hand (input field)
submit button
how test , send error if user doesn't select radio button want return profit amount (price - cost) should auto generated on second page script. here script far (i'm using notepad++ editor)
#!/usr/bin/perl print "content-type: text/html\n\n"; use cgi qw(:standard); local ( $buffer, @pairs, $pair, $name, $value, %form ); # read in text $env{'request_method'} =~ tr/a-z/a-z/; if ( $env{'request_method'} eq "post" ) { read( stdin, $buffer, $env{'content_length'} ); } else { $buffer = $env{'query_string'}; } # split information name/value pairs @pairs = split( /&/, $buffer ); foreach $pair ( @pairs ) { ( $name, $value ) = split( /=/, $pair ); $value =~ tr/+/ /; $value =~ s/%(..)/pack("c", hex($1))/eg; $form{$name} = $value; } #variables input text fields $item = $form{item}; $name = $form{name}; $cost = $form{cost}; $price = $form{price}; $quantity = $form{quantity}; #radio button $letter = $form{letter}; #validate data entered in first 2 input boxes if ( $item eq "" ) { print "$item field cannot blank"; } if ( $name eq "" ) { print "$name field cannot blank"; } #validate correct amount entered if ( $cost >= .50 && $cost <= 1000 ) { print "$cost cost must between $.50 , $1000"; } if ( $price >= 1.00 && $cost <= 2000 ) { print "$price price must between $1.00 , $2000"; } #validate category chosen #validate quantity on hand not less 0 if ( $quantity < 0 ) { print "$quantity quantity on-hand cannot less 0"; }
assuming want stick cgi technology (which, frankly, ridiculous in 2015) code can simplified this.
#!/usr/bin/perl use strict; use warnings; use cgi qw(:standard); @errors; #validate data entered in first 2 input boxes foreach $param (qw[item name]) { if ( ! defined param($param) ) { push @errors, "$param field cannot blank"; } } #validate correct amount entered $cost = param('cost'); if ( $cost >= .50 && $cost <= 1000 ) { push @errors, 'cost must between $.50 , $1000'; } $price = param('price') if ( $price >= 1.00 && $price <= 2000 ) { push @errors, 'price must between $1.00 , $2000'; } #validate quantity on hand not less 0 if ( param('quantity') < 0 ) { push @errors, 'quantity on-hand cannot less 0'; } print header; # need send html response user. if have errors # in @errors, need send them page describes # have got wrong. if @errors empty, should send them page # containing whatever information need next. if (@errors) { # create , send error page } else { # create , send next page }
Comments
Post a Comment