On Sun, Mar 08, 2009 at 03:03:33PM +1100, Naz wrote:
>
> Generally, the approach I take is as follows:
>
> $errors = array();
> if (empty($_POST['name']))
> array_push($errors, 'A name was not entered.');
> if (empty($_POST['address']))
> array_push($errors, 'An address was no supplied.');
> if (empty($_POST['gender']) || ($_POST['gender'] != 'M' &&
> $_POST['gender'] != 'F'))
> array_push($errors, 'A gender was not supplied.');
>
> And then you can check that all form fields were correctly filled in by
> checking for:
>
> count($errors) == 0
>
> If that is not true, simply output the contents of the $errors array
> appropriately. The advantages of doing it like this are:
>
> 1. You get full details on why the form was not filled out, rather
> than simply telling users "The form was not filled in" and not
> telling them what fields they missed or what was incorrectly
> filled out.
> 2. You can set multiple criteria over and above simply checking for a
> value, e.g., ensuring that the gender field is either M or F and
> not an invalid value (in practice this will probably be a radio or
> drop list to ensure correct values, but this method ensures that
> you can do value sanity checking as well for things like dates etc)
>
>From the php.net manual for array_push():
Note: If you use array_push() to add one element to the array it's
better to use $array[] = because in that way there is no overhead
of calling a function.
But that's the only change I would make. Overall, I completely agree
with this philosophy.
Paul
--
Paul M. Foster
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php