Re: if elseif elseif elseif....

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



PJ wrote:
This is probably a mysql question, but their list is rather dull - I
think they don't appreciate my humor. Beside this list is fun ... and
informative.
Anyway, I can't figure this out. I am trying to verify inputs on a form
and even if I have all the required fields right, I still get the error
that there are empty obligatory fields.
This is what I am using... but I'm open to anything that works or is
more efficient:
if (strlen($_POST["titleIN"]) == 0 ) {
            $obligatoryFieldNotPresent = 1;
    }
    elseif (strlen($_POST["first_nameIN"]) == 0 ) {
            $obligatoryFieldNotPresent = 1;
    }
    elseif (strlen($_POST["publisherIN"]) == 0 ) {
            $obligatoryFieldNotPresent = 1;
    }
    elseif (strlen($_POST["copyrightIN"]) == 0 ) {
            $obligatoryFieldNotPresent = 1;
    }
    elseif (strlen($_POST["ISBNIN"]) == 0 ) {
            $obligatoryFieldNotPresent = 1;
    }
    elseif (strlen($_POST["languageIN"]) == 0 ) {
            $obligatoryFieldNotPresent = 1;
    }
    elseif (strlen($_POST["categoriesIN"]) == 0 ) {
            $obligatoryFieldNotPresent = 1;
    }
    elseif ($obligatoryFieldNotPresent = 1) {
            $obligatoryFieldNotPresent = 0;
    }

I've tried closing with
else ($obligatoryFiieldNot Present = 0);
end;
but that give me a blank page - haven't figure out how to check for
parsing or scripting errors
Thanks for any suggestions.

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)

Hope that helps!
- Naz.


--
?????? ?????

Web:  www.mrnaz.com
Ph:   +61 400 460 662


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux