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!
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? :\
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.
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? :
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."
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.
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...
<?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
}
}
?>
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!