Best method to report error to end users?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
senthil
Forum Newbie
Posts: 12
Joined: Wed Jan 15, 2003 6:42 pm

Best method to report error to end users?

Post by senthil »

Hi,

In my website, if any user enters wrong/invalid input, i pass a error number to a page, like 'error.php?err=2'

In error.php page I have a big list (around 60) of error messages, like invalid username, invalid email...etc. Based on the err# I display the appropriate message.

But it is becoming difficult for me to maintain the code, if I want to change anything. Could anyone tell me how to do this in a more efficient way.

Thank you
senthil
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

I don't know if it's any easier to maintain, but I often post a form to its own page and validate there, and redirect or include the "next" page from that.. this way it's easy to show the user-fault by the associated field..

Code: Select all

<?php
  $err = array();
  $ok = 0;

  if (!empty($_REQUEST['action_subm_someform'])) {

     # validate stuff here
     if (strlen($formval1 = stripslashes($_REQUEST['formval1'])) < 6) {
        $err['formval1'] = 'Your uggabuggah must be at least 6 characters';

    }
   
    # If no error, include (or you could use redirect) the 'next page'
    if (!$err) {
      include('/the/next/file');
      $ok = 1;
    }
  }
  
  if (!$ok) {

    # Main page/form
    ?>
    <form action="thisfile.php" method="POST">
     <?php 
        echo $err['formval1'] ? '<font color="#FF0000">'.$err['formval1'].'</font><br />' : '';
     ?>
     <input type="text" name="formval1" value="<?php echo $formval1; ?>">
     <br />
     <input type="hidden" name="action_subm_someform" value="1">
       <!-- The above line is to make sure form submission is catched, some browsers dont pass the submit field value when users hit enter instead of clicking the button -->
     <input type="submit" name="submit" value="Submit">
    </form>
  <?php

 }
?>
Post Reply