Click to See Complete Forum and Search --> : ereg function not working


christopher
01-10-2004, 11:25 AM
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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>&nbsp;</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\">&nbsp;</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\">&nbsp;</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* $k2 * $fat;
$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* $k2 * $fat;
$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.-->

Jona
01-10-2004, 06:28 PM
Doesn't ereg() need a delimiter? Like / or #?

[J]ona

pyro
01-10-2004, 07:25 PM
Originally posted by Jona
Doesn't ereg() need a delimiter? Like / or #?Nope. ;)

Christopher, you might be interested in the is_numeric() (http://us2.php.net/is_numeric) function, as there is really no need to invoke a regex engine for what you are trying to do.

Also, you said: "In theory the subs () function should work for the assigned string length, but let me tell you: IT DOESN'T."

There is no subs() function, did you mean substr()? If so, what makes you say it should work?

christopher
01-11-2004, 12:10 AM
Okay, I finally found the problem with the ereg function it was missing a ^ and now it is working. I am glad you told me about the is_numeric function. I had no idea that it existed, but now that I know I will put it to use.
yes I did spell substr() function wrong. Sorry. I got the idea for its use from Bill Pellowe at
http://www.eltcalendar.com on his mileage script. He uses the substr() function to check that the first character written is not a letter. But then I found ereg can do the samething. thanks for all the help.

pyro
01-11-2004, 09:40 AM
Originally posted by christopher
He uses the substr() function to check that the first character written is not a letter. But then I found ereg can do the samething.Yep, for something like that, it'd be easier to use regex than substr(). One thing to keep in mind - PCRE are much more powerful than POSIX regex, so if you need to do any serious work, that's probably what you'll want to use.