[Solved] Having trouble with a class...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
The Bat
Forum Newbie
Posts: 14
Joined: Thu Feb 01, 2007 3:57 pm

[Solved] Having trouble with a class...

Post 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!
Last edited by The Bat on Sat Feb 03, 2007 7:53 pm, edited 1 time in total.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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.
User avatar
xinnex
Forum Commoner
Posts: 33
Joined: Sun Jan 07, 2007 7:38 pm
Location: Copenhagen, Denmark

Post by xinnex »

Don't know wether the code is copy+paste or not..
But this line needs some attention:

Code: Select all

$myform = new fVal;
Should be:

Code: Select all

$myform = new fVal();
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

xinnex wrote:Don't know wether the code is copy+paste or not..
But this line needs some attention:

Code: Select all

$myform = new fVal;
Should be:

Code: Select all

$myform = new fVal();
Not really, if the constructor does not take parameters then new fVal; will work just fine...
The Bat
Forum Newbie
Posts: 14
Joined: Thu Feb 01, 2007 3:57 pm

Post 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? :\
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
xinnex
Forum Commoner
Posts: 33
Joined: Sun Jan 07, 2007 7:38 pm
Location: Copenhagen, Denmark

Post 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.
The Bat
Forum Newbie
Posts: 14
Joined: Thu Feb 01, 2007 3:57 pm

Post 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) { ...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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."
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.';
}
?>
The Bat
Forum Newbie
Posts: 14
Joined: Thu Feb 01, 2007 3:57 pm

Post 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. :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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
    }
}
?>
The Bat
Forum Newbie
Posts: 14
Joined: Thu Feb 01, 2007 3:57 pm

Post 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. :oops: This works perfectly, thank you!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You're welcome. I'm glad I could be of assistance.
Post Reply