On Thu, April 20, 2006 9:52 pm, benifactor wrote:
> I am trying to come up with the best way to check for errors on a
> form.  I have done this before by checking each $_POST variable
> seperatly but needed a better way that would display more then one
> error.  The following is what i came up with...
>
> if (!$condition1 || !$condition2 || $condition1 != $condition2) {
>     if (!$condition1) {
>      $regError[0] = "condition 1  not met.";
>      $regErrorc[0] = "<font color=red>";
>      $regErrore[0] = "</font>";
>      $regErrord[0] = "<font color=red>***</font>";
>     }
>     else {
>     if (!$condition2) {
>      $regError[1] = "condtion 2 not met.";

Well, you need to use .= in a lot of places here, but it's not a bad
start.

You're also mixing your presentation junk of font tags into the logic,
which is probably not-so-good.

It's also too likely that, over time, you'll mess up and duplicate an
index like [24] and [24] for two different errors, if you haven't
already.

But your basic idea is sound.

So let's look at a simpler version:

Set up a 'globals.inc' file which initializes your $messages array,
and has the code to put out your masthead and navigation:

globals.inc:
<?php
  $messages = array();

  function head($title = 'My Site!'){
    //I hate global as much as the next guy, but...
    //I can live with it for an error-output array of messages.
    global $messages;
?>
<html>
  <head>
    <title><?php echo $title?></title>
    <meta keywods blah blah blah>
    <meta description blah blah balh>
  </head>
  <body>
    <!-- navigation bar and logo go here -->
    <?php if (count($messages)){
       echo "<font color=red>", implode("<br />", $messages), "</font>";
    }?>
<?php
  }

  function foot(){
?>
  </body>
</html>
<?php
  }
?>


Then, in your usual files, you would do:
<?php
  require 'globals.inc';

  if ($condition1){
    //Note that with no index, PHP just tacks the message on to the
end of the array. This is better than using an index.
    //Because you may find yourself adding error messages in include
files, and you will get confused about which number to use.
    $messages[] = "condition 1 not met";
  }
  if ($condition2){
    $messages[] = "condition 2 not met";
  }

  head('My Site Rocks!');
?>
<p>This is your content layed out.</p>
<p>You still need to be careful not to output Bad Things when
condition1 failed above. But your error message is taken care of.</p>
<?php
  foot();
?>


This solution is a lot like what you had, but with a lot less clutter.

It's also one I've been using for years for simple boutique sites.

It's not as rigid in separation of content and presentation as some
would like, but it's not a total mess in that regard either.  The
basic business logic is at the top of each file, the layout of the
main content is in the file, and the shared structure (what little
there is) is in the globals file for all files to, well, share.

I'd personally consider using CSS instead of FONT tags unless you need
ancient browser support.  (And some of us do!)

But with this setup, you'd have a minimal number of changes to go that
route.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to