Jump to content

Form validation class not working properly.


Guest convention

Recommended Posts

Guest convention
I'm fairly new to OOP programming in PHP, so I need some help working this out.

I created a class which contains a function to validate a regular form input field. Notice how in the class below, the first if statement determines whether the form button is pressed or not. When I instantiate the class and use the if statement (if ($check)...), the 'Error.' echoes even before the form button is pressed, even though I made it clear to check that in the class.

[code=php:0]
class formVal {
  var $button;
  var $field;
  var $len;

  function allChars() {
      if (isset($this->button)) {
  if (strlen($this->field) >= $this->len) {
      return (true);
  } else {
      return (false);
          }
}
  }
}
[/code]
---------------------------------------------------------------------
[code=php:0]
$testform = new formVal;
$testform->button = $_POST['submit']; $testform->field = $_POST['field1']; $testform->len = 5;
$check = $testform->allChars();
if ($check) {
  echo 'Passed!';
} else {
  echo 'Error.';
}
[/code]

How can I fix this problem?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.