I know there are more elegant ways to set up an html form to take the input of male or female; however, I am learning, and I was just wondering if this is also possible:
//Incorporate two checks
if (empty($_POST['sex'])==FALSE && sanityCheck($_POST['sex'], 'string', 1)==TRUE)
{
// if the checks are ok for sex we assign sex to a variable
$sex = $_POST['sex'];
}
else
{
// if all is not well we echo an error message
echo 'Please enter a valid Gender';
// and exit the script
exit();
}
If so, how would I check this with regex?
Whether the user typed M or F.
I am thinking:
function checksex($sexcheck)
{
//Some regex here to check the sex?
return preg_match(' ', $sexcheck);
}
And then call checksex as a third check added to the if conditional, like this:
if (empty($_POST['sex'])==FALSE && sanityCheck($_POST['sex'], 'string', 1) != FALSE && checksex($_POST['sex'], 1) ==TRUE)
{
...
}
To check if the user typed M or F, all you would need to do is a string comparison. No need for regex
However, rather than having the user type out M or F, why not use a select box or Radio Buttons ?
A select box may look something like this
Or radio buttons:
You would of course still have to check that
$_POST['sex']is still M or F, and handle the case where it isn’t either of them, as the user may decide to post an invalid value.