I am trying to validate a form using a series of if else statements. The
user must fill out the required fields and submit proper input before an
acknowledgement page is returned thanking the user. The code I have
implemented checks to see if each post variable contains valid input and
if not, returns an error message. So far so good. I hit a snag when I
try to implement validation for a non-required field such as Middle
Initial. The code I have written looks at $_POST[MI] and, if not empty,
sees if it consists of a single alphabetical character. If not a single
alphabetical character, an error message is returned. If the user decides
to enter in an ASCII character or a number, an error message is returned.
The problem I am having is when the user enters an alphabetical character,
the program seems to just stop.
I feel this is a simple if else syntax issue, but
maybe not...
I have included the relevant piece here:
else
if (!empty($POST[MI]))
{
if (preg_match('/[[:alpha:]]{1,1}$/', $POST[MI]) == 0)
{
echo "hello 1";
?>
<P>You must specify a Middle Initial composed of an alphabetical
character</P>
<?php
echo "hello 2";
}
else
{
echo "hello 3";
}
echo "hello 4";
}
I have added a series of tests to see where the program goes wrong.
Assuming the user has entered in the required fields previous to this, if a nonvalid character (e.g., % or 7) is entered in the middle initial box,
it prints out:
hello 1
You must specify a Middle Initial composed of an alphabetical character
hello 2hello 4
If I leave it blank it works just fine. Soon as I enter in a capital P, it prints out:
hello 3hello 4
and just stops. It doesn't go on with the rest of the program ! Do you
know what I might be doing wrong ?