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

what is the logic of outputting cgi errors below the login or sigup form below

Replies are listed 'Best First'.
Re: cgi error output
by vinoth.ree (Monsignor) on Apr 15, 2015 at 13:14 UTC
    Hi bigup401,

    Is that you want to display error in browser?

    You need to install and use CGI::Carp module.
    
    With this module the standard warn(), die (), croak(), confess() and carp() calls will automatically be replaced with functions that write out nicely time-stamped messages to the HTTP server error log.
    
    You can also log message to a browser. Now add following two lines before sending any headers to a browser:
    use CGI;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser); 
    
    With above lines it is possible to make non-fatal errors appear as HTML comments embedded in the output of your program. To enable this feature, export the new "warningsToBrowser" subroutine. Since sending warnings to the browser before the HTTP headers have been sent would cause an error, any warnings are stored in an internal buffer until you call the warningsToBrowser() subroutine with a true argument
    
    Update:

    suppose if this not you wanted and want to display your error message below the login or signup form, here is some of the suggestion.

    Once the your submitted the form, go for user input_validation(), get the user input and validate, have a variable and save error message in that variable if found any, at the end of input_validation() check error variable, if any error occurs call another function which displays the form again and print the error message below the form.

    Ex:

    sub input_validation { my $your_name = $query->param("user_name"); my $your_sex = $query->param("user_sex"); my $error_message = ""; # holds error message #Here do whatever validation you want to do $error_message .= "Please enter your name<br>" if ( !$user_name ); $error_message .= "Please specify your sex<br>" if ( !$user_sex ); if ( $error_message ) { show_login_form ( $error_message, $user_name, $user_sex ); return 0; } else { # Form OK return 1; } } sub show_login_form { my ($error_message,$user_name,$user_sex) = @_; #Add user login or signup HTML content here.. <form> ..... ...... <p>$error_message</p> </form> }

    All is well. I learn by answering your questions...

      i tried that before bt never worked for me

      here is the simple

      $usr = $cgi->param('textfield);
      $pwd = $cgi->param('textfield2);
      if (! $usr) { # print error }
      print <<HTML;
      <html> <head> <body> <p><strong>Username <input type="text" name="textfield" /> </strong></p> <p> Password <input type="text" name="textfield2" /> </p> <p> <input type="submit" name="Submit" value="Submit" /> # LET THE ERROR BE SHOWN HERE </p> </body> </html> HTML;
        my $error = $usr ? '' : 'Gah! No usr supplied!'; print <<HTML; <html> <head> <body> <p><strong>Username <input type="text" name="textfield" /> </strong></p> <p> Password <input type="text" name="textfield2" /> </p> <p> <input type="submit" name="Submit" value="Submit" /> <div class="error">$error</div> </p> </body> </html> HTML

        ... for example, but yes templates would probably be much better.

        What kind of error do you mean?

        Maybe you want to learn about how to output HTML in parts? Maybe a templating system is useful to separate the HTML you generate from your Perl code?

        Maybe you want to just

        if( $errorMessage ) { print $errorMessage; }

        ... in the right place?