in reply to Why is my CGI.pm script trying to assign data before pressing submit.

The posts above did a fine job of already explaining what (looks to be) wrong. However, another solution for this would be a bit of JavaScript instead of Perl (am I allowed to say that here?). Actually, you need both especially since JavaScript doesn't work on all browsers. Whatever script gets the input from the form needs to check the entry but you can be nice to your users and warn them as soon as they hit "submit." Try the following JavaScript:
function checkform (form) { if (form.report.value == "") { alert("You need to enter a filename!"); form.report.focus; return false; } return true; }

Inside your initial form tag (I'm not sure how to add this with CGI.pm), you'll need to add:
onsubmit="return checkform(this);"
Hope this helps,
Ardenstone

Replies are listed 'Best First'.
Re: Re: Why is my CGI.pm script trying to assign data before pressing submit.
by CharlesClarkson (Curate) on Dec 07, 2001 at 20:30 UTC

    With CGI.pm you would need these:

    use CGI; my $p = new CGI; my $js_func = qq| function checkform (form) { if (form.report.value == '') { alert('You need to enter a filename!'); form.report.focus; return false; } return true; } |;

    Place the script in $p->start_html using -script.

    print $p->start_html( -script => $js_func );

    Then, when you start the form, use:

    print $p->start_form(-onsubmit => 'checkform(this)');


    HTH,
    Charles