Hello, everyone. I am having a problem with an ereg function
in my all in one php form.
The script has a text input for someone to enter their percent bodyfat (without the % sign) and I am trying to create an error statement so that if someone enters either a % or letters
a warning will come saying:
“You have entered either a % sign or letters instead of numbers. Please check and submit again.”
Without this error statement, the form works fine, so it is definitely the ereg function. Please don’t suggest that I use
the subs() function, because It only works for the first letter. In theory the subs () function should work for the assigned string length, but let me tell you: IT DOESN’T.
well anyhow the script looks like this:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“
<html>
<head>
<title></title>
</head>
<body>
<table width=”100%” height=”100%” cellpadding=”0″ cellspacing=”0″ border=”0″ align=”center”>
<tr>
<td width=”100%” height=”100%” valign=”middle”>
<?
$form_block = “
<form method = “post” action = “$PHP_SELF”>
<table border=0 cellpadding=5 cellspacing=0 >
<tr>
<td><b>Name</b></td>
<td colspan=6><input type=text size=35 maxlength=25 name=name /></td>
</tr>
<tr>
<td ><b>Bodyweight (lbs)</b></td>
<td colspan=6><input type=text size=8 maxlength=3 name=weight /></td>
</tr>
<tr>
<td><b>Sex</b></td>
<td colspan=6>
<select name=gender>
<option value=male >Male</option>
<option value=female selected>Female</option>
</select>
</td>
</tr>
<tr>
<td><b>% Bodyfat</b><br /><font size=”1″>(without the % sign)</font></td>
<td><input type=”text” size=”5″ maxlength=”2″ name=”fat” /></td>
</tr>
<tr><td colspan=7 height=50> </td></tr>
<input type=submit name=submit value=Calculate size=20></td>
</tr>
</table>
</form>
“;
// Check that all input fields are filled.
if (($name == “”) || ($weight == “”) || ($fat == “”))
{
echo “
<table border=”0″ cellspacing=”0″ cellpadding=”0″ width=”100%” align=”center”>
<tr><td height=”20″ align=”center”><b>One or more fields were left blank. Please check and submit the form again.</b></td></tr>
<tr><td height=”20″ align=”center”><hr width=”100%” align=”center” /></td></tr>
<tr><td height=”20″ align=”center”> </td></tr>
</table>”;
echo “$form_block”;
}
// Check there is no percent sign or letters in bodyfat.
else if (ereg(“([0-9])” , $fat)) {
echo “
<table border=”0″ cellspacing=”0″ cellpadding=”0″ width=”100%” align=”center”>
<tr><td height=”20″ align=”center”><b>You have entered a letter(s) or a percent % sign in place of a number. Please check and submit the form again.</b></td></tr>
<tr><td height=”20″ align=”center”><hr width=”100%” align=”center” /></td></tr>
<tr><td height=”20″ align=”center”> </td></tr>
</table>”;
echo “$form_block”;
}
else {
if ($gender == “female”){
$k1 = .9;
$k2 = 24;
{
if ($fat <= 18) {$fat = 1.0;}
else if (($fat >18) && ($fat <= 28)) {$fat = .95;}
else if (($fat >28) && ($fat <= 38)) {$fat = .90;}
else {$fat = .85;}
}
$mass= $weight/(2.2050);
$bmr= $mass * $k1
$roundbmr= round($bmr);
}
else{
$k1 = 1;
$k2 = 24;
{
if ($fat <= 14) {$fat = 1.0;}
else if (($fat >14) && ($fat <= 20)) {$fat = .95;}
else if (($fat >20) && ($fat <= 28)) {$fat = .90;}
else {$fat = .85;}
}
$mass = $weight/(2.2050);
$bmr = $mass * $k1
$roundbmr = round($bmr);
}
echo “
<table border=”0″ cellpadding=”5″ cellspacing=”0″ width=”100%” height=”100%” align=”center” class=”result”>
<tr>
<td align=”left” valign=”middle”>
$name, your body needs a minimum intake of $roundbmr calories/day to function properly.
This is your BMR (Basic or Basal Metabolic Rate) and its calculations were based on your gender and your
bodyweight of $weight lbs.
</td>
</tr>
</table>”;
}
?>
</td></tr></table>
</body>
</html>
<!–In this version of percent, I removed the hidden input precedent to the submit button
to process the form. By simplifying the form in this way I am isolating the ereg function and
testing it to see how it interacts with other functions and control statements.
As I figure this out, I will make the form more complicated again.–>
<!–when I tested the form, I got the ereg error statement regardless if I entered a single or
double digit, therefore the ereg range is not wrong, but how it’s format or location in the form–>
<!–In trial3 I added an exit(); to the ereg. Upon testing it I saw that the mistake remained–>
<!–In trial4 I got rid of the ereg statements to see if the form even works without it.–>
<!–Okay, the form works without ereg function, so that is the problem–>
<!–In trial5, I redid the parentheses the way Jonathan at crosspoints.org had it in the ereg
function.There is an error and it is not working. I am taking the form to a php forum.–>