Dear perlmonks:

I have a problem.I use java script function validateForm() to check form input:

print start_multipart_form(-name=>'form1', -onSubmit=>"validateForm()", -method=>"post"),"\n";
it can work ,but cannot return original page when error.
so I change onSubmit=>"return validateForm()",but it cannot get form params.Why?
I use Apache 1.3.12 and IE5, the script as following :
#!/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

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.