Page 1 of 1
[Solved] Having trouble with a class...
Posted: Thu Feb 01, 2007 4:05 pm
by The Bat
Hello,
I am kind of new to PHP classes, and I'm having trouble with a dummy class I just wrote. It's for validating a form field.
Code: Select all
class fVal {
function checkString($field, $submit, $snum, $bnum) {
if ($submit && $field >= $snum && $field <= $bnum) {
return (true);
} else {
return (false);
}
}
}
$myform = new fVal;
if ($myform->checkString($_POST['field1'], $_POST['submit'], 4, 20)) {
echo 'Accepted.';
} else {
echo 'Denied.';
}
No matter what is input, I always get 'Denied.'. Why isn't it working? Thank you for your help!
Posted: Thu Feb 01, 2007 6:00 pm
by Skara
The class looks fine. If you're not getting any errors from it, then check the rest of your code. Probably something wrong with the input.
Posted: Thu Feb 01, 2007 6:15 pm
by xinnex
Don't know wether the code is copy+paste or not..
But this line needs some attention:
Should be:
Posted: Thu Feb 01, 2007 6:19 pm
by nickvd
xinnex wrote:Don't know wether the code is copy+paste or not..
But this line needs some attention:
Should be:
Not really, if the constructor does not take parameters then new fVal; will work just fine...
Posted: Fri Feb 02, 2007 12:48 pm
by The Bat
Skara wrote:The class looks fine. If you're not getting any errors from it, then check the rest of your code. Probably something wrong with the input.
Well, that's the only PHP code I have on that page, as well as a form. But even before I submit the form, it says 'Denied'. Anyone know what's going on? :\
Posted: Fri Feb 02, 2007 2:38 pm
by shiznatix
The Bat wrote:Skara wrote:The class looks fine. If you're not getting any errors from it, then check the rest of your code. Probably something wrong with the input.
Well, that's the only PHP code I have on that page, as well as a form. But even before I submit the form, it says 'Denied'. Anyone know what's going on? :\
well of course even if you dont hit submit it will day 'denied' because the code is processed on each page load. so when you go there, it checks if the stuff exists and runs through it and since it does not even exist yet it fails validation and returns false.
wrap this part like this:
Code: Select all
if (count($_POST))
{
$myform = new fVal;
if ($myform->checkString($_POST['field1'], $_POST['submit'], 4, 20))
{
echo 'Accepted.';
}
else
{
echo 'Denied.';
}
}
that will check to see if you posted anything yet before doing the check string
Posted: Fri Feb 02, 2007 3:26 pm
by xinnex
nickvd wrote:Not really, if the constructor does not take parameters then new fVal; will work just fine...
Arh, me and my Java-based assumptions.. my bad.
Posted: Fri Feb 02, 2007 3:44 pm
by The Bat
shiznatix wrote:
well of course even if you dont hit submit it will day 'denied' because the code is processed on each page load. so when you go there, it checks if the stuff exists and runs through it and since it does not even exist yet it fails validation and returns false.
Shouldn't that not happen because of the if statement in the function? :
Code: Select all
if ($submit && $field >= $snum && $field <= $bnum) { ...
Posted: Fri Feb 02, 2007 4:01 pm
by superdezign
Getting "Denied" every time means that ($submit && $field >= $snum && $field <= $bnum) is always resulting as false.
What's the problem? Well if you KNOW $field >= $snum is true, then it's not that. If you KNOW $field <= $bnum is true, then it's not that. And if you KNOW $submit is true, its not that. So how do you figure this out? Test each condition separately.
My guess is that you don't give a value to your submit button, and because your checking for the button itself rather than isset($submit), it interprets the lack of a value as "", which, last time I checked, is basically "false."
Posted: Fri Feb 02, 2007 6:47 pm
by RobertGonzalez
This is not as flexible as you'll eventually want it, but try this and see what it does.
Code: Select all
<?php
class fVal
{
var $error = '';
function checkString ($post_field, $min, $max)
{
if (!isset($_POST[$post_field]))
{
$this->error = 'The post field ' . $post_field . ' was not set.';
return false;
}
$field = $_POST[$post_field];
if ($field >= $min && $field <= $max)
{
return true;
}
else
{
$this->error = 'The post field ' . $post_field . ' was out of range.';
return false;
}
}
function get_error()
{
return $this->error;
}
}
$myform = new fVal;
$check = 'field1';
if (!$myform->checkString($check, 4, 20))
{
echo $myform->get_error();
}
else
{
echo 'Sweet.';
}
?>
Posted: Sat Feb 03, 2007 3:41 pm
by The Bat
Everah wrote:This is not as flexible as you'll eventually want it, but try this and see what it does.
Hmm, when I first open the script, it says "The post field field1 was not set." And, no matter what I input (despite my input being over 4 characters) it says "The post field field1 was out of range." It's all right though, it's not the end of the world if I don't figure what is wrong with it. I do appreciate everyone helping me out, though. Thank you.

Posted: Sat Feb 03, 2007 3:46 pm
by Ollie Saunders
Posted: Sat Feb 03, 2007 5:53 pm
by RobertGonzalez
The code I posted (which is a revamped version of yours) does not check string length, it checks numeric value. If you want string length, the you need to run the string through
strlen(). Try this one, it should get rid of the error notice and the check string length...
Code: Select all
<?php
class fVal
{
var $error = '';
function checkString ($post_field, $min, $max)
{
if (!isset($_POST[$post_field]))
{
$this->error = 'The post field ' . $post_field . ' was not set.';
return null;
}
$size = strlen($_POST[$post_field]);
if ($size >= $min && $size <= $max)
{
return true;
}
else
{
$this->error = 'The post field ' . $post_field . ' was out of range.';
return false;
}
}
function get_error()
{
return $this->error;
}
}
$myform = new fVal;
$check = 'field1';
if (($ret = $myform->checkString($check, 4, 20)) === false)
{
// The method returned false, which is out of range
echo $myform->get_error();
}
else
{
if (!is_null($ret))
{
echo 'Sweet.';
}
else
{
// This means the form field was not set
// You can do what ever you want here or skip it altogether
}
}
?>
Posted: Sat Feb 03, 2007 7:52 pm
by The Bat
Everah wrote:The code I posted (which is a revamped version of yours) does not check string length, it checks numeric value. If you want string length, the you need to run the string through
strlen(). Try this one, it should get rid of the error notice and the check string length...
Wow, I completely forgot about strlen.

This works perfectly, thank you!
Posted: Sun Feb 04, 2007 4:55 pm
by RobertGonzalez
You're welcome. I'm glad I could be of assistance.