On Wed, 04 Mar 2009 17:51:11 -0500, [email protected] (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;
Rather than encoding all the variable names into one long unwieldy set of
statements, I
would put them all into an array, and then use a loop to process the array.
This way all
the variable names are together, and the next time you want to enter another
set of
variables you can use the same code, but read from a different array.
For example:
$oblig_fields = array('TitleN', 'first_nameN', '....');
$ok = true;
$i = 0; while ($i < count($oblig_fields && $ok))
{
if (!isset($_POST[$oblig_fields[$i]]) || ($_POST[$oblig_fields[$i]] ==
''))
{
$ok = false;
}
else
{
$results[$oblig_fields[$i]] = $_POST[$oblig_fields[$i]];
}
++$i;
}
if (!$ok) { echo 'Scream!!!'; }
once you have the basic code working, you can easily add extra frills. For
example if only
some of the fields were compulsory, you could have one array $all_fields, and
another
$oblig_fields. Then, as you processed each entry using $all_fields, you would
only break
one missing data if that key was set in $oblig_fields.
I am sure you could use a "break" statement in place of the "$ok = false;"
above, but I
have never trusted the break statement. To my mind it is like a GOTO without a
target. As
I used to tell my students "When I say break, the only thing I can be certain
of is that
the lecture theatre will be empty in 30 seconds. I have no idea where most of
you go, and
I don't know if I will ever see some of you again."
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php