I have a problem.I use java script function validateForm() to check form input:
it can work ,but cannot return original page when error.print start_multipart_form(-name=>'form1', -onSubmit=>"validateForm()", -method=>"post"),"\n";
#!/usr/local/bin/perl # Use JavaScript to validate fill-out # forms. use CGI qw(:standard); # Here's the javascript code that we include in the document. $JSCRIPT=<<EOF; // validate that the user is the right age. Return // false to prevent the form from being submitted. function validateForm() { var ret = validateEmail(document.form1.email); if (! ret) { return false; } ret = checkFirstName(document.form1.firstname); if (! ret) { return false; } ret = checkLastName(document.form1.lastname); if (! ret) { return false; } //document.form1.state = 'valid'; //document.forms[0].submit(); return true; } // make sure that the contents of the supplied // field contain a valid date. function validateEmail(element) { //alert(element.value); if (element.value.indexOf("@")<0 ) { alert("Please valid your e-mail address"); element.focus(); element.select(); return false; } return true; } function checkFirstName(element) { if (element.value.length == 0 ) { alert(element.name+"can not be null."); element.focus(); } } function checkLastName(element) { if (element.value.length == 0 ) { alert(element.name+"can not be null."); element.focus(); } } EOF ; # here's where the execution begins print header; print start_html(-title=>'Untitled Document', -head => meta({ -http_equiv => 'Content-Type', -content => 'text/html; charset=iso-8859-1'}), -script=>$JSCRIPT); print_prompt(); print_response() if (param); print end_html; sub print_prompt { #post return print start_multipart_form(-name=>'form1', -onSubmit=>"validateForm()", -method=>"post"),"\n"; print table({-border=>0,-width=>"75%"},"\n", Tr(td({-width=>"32%"},div({-align=>"right"},'First Name: ')) , +td({-width=>"68%"},input({-type=>'text',-name=>'firstname'})) ), Tr( [ td([div({-align=>"right"},'Last Name:'),input({-type=>'text +',-name=>'lastname'})]), td([div({-align=>"right"},'Email Address: '),input({-type=> +'text',-name=>'email'})]), td([div({-align=>"right"},'File One: '),input({-type=>'file +',-name=>'fileone'})]), td([div({-align=>"right"},'File Two: '),input({-type=>'file +',-name=>'filetwo'})]), td([div({-align=>"right"},'File Three: '),input({-type=>'fi +le',-name=>'filethree'})]) ]) ); print hidden('state','init'); print submit(),"\n"; print end_form; } sub print_response { return unless(param('firstname')); return unless(param('lastname')); $value = param('email'); return unless($value =~/@/); my $i=0; my $SAVE_DIRECTORY = "c:/temp"; foreach $key (param()) { next unless $key =~ /file/; my $file_name = param($key); next unless length($file_name)>0; $file_name =~ /([^\\\/]+)$/; $SaveFilename = $1; unless (open(OUTFILE, ">$SAVE_DIRECTORY/$SaveFilename")){ print "Cannot open file $SAVE_DIRECTORY/$SaveFilename for + upload.\n",p;; next; } while ($Bytes = read($file_name,$Buffer,1024)) { print OUTFILE $Buffer; } close(OUTFILE); close($file_name); print "Upload file to $SAVE_DIRECTORY/$SaveFilename.\n",p; $i++; } print "Total file count $i .\n"; }
In reply to A problem use CGI.pm by lgjut
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |