I'm new to OOP in PHP and am trying to write a class to handle form generation and error handling. I would like to check the $HTTP_POST_VARS variable to see if the form has been submitted. In my class I tried checking the count($HTTP_POST_VARS) but got an error as the $HTTP_POST_VARS was undefined. To work around this I tried an IsSet($HTTP_POST_VARS), but it never evaluates as true even when the form has been posted. I tried a quick test to check $HTTP_POST_VARS using a normal form submission, and it seemed to work. However when I try to drop the same code into my class it doesn't. Here are code snippets from the three files I'm using. If anyone can help I'd be eternally grateful.
============= formprocessor.php =============
class FormProcessor {
// class attributes
var $name; // form name
var $page; // form page name
var $errors = array(); // associative array of error messages
var $values = array(); // associative array of form variables
// class constructor
function FormProcessor($name, $page) {
$this->name = $name;
$this->page = $page;
if(IsSet($HTTP_POST_VARS)) {
print("submitted");
} else {
print("not submitted");
}
}
function formStart() {
print("<form method=\"post\" action=\"$this->page\">\n");
}
function formEnd() {
print("</form>\n");
}
// end class
}
============= formcontrols.php =============
class FormControls {
// class attributes
var $name; // form element name
var $value; // form element value
var $attributes; // form element attributes
// class constructor
function FormControls($name, $value, $attributes) {
$this->name = $name;
$this->value = $value;
$this->attributes = $attributes;
$this->render();
}
function render() {
// abstract function for overriding
}
// end class
}
class TextInput extends FormControls {
function render() {
print("<input type=\"text\" name=\"$this->name\" $this->attributes>\n");
}
}
class SubmitButton extends FormControls {
function render() {
print("<input type=\"Submit\" name=\"$this->name\" value=\"$this->value\">\n");
}
}
}
============= examplepage.php =============
<body>
<?php
require('./formprocessor.php');
require('./formcontrols.php');
$searchForm = new FormProcessor("Search", "example.php");
$searchForm->formStart();
$myName = new TextInput("myName", "Brian", "size=5");
$myButton = new SubmitButton("Submit", "Submit", "");
$searchForm->formEnd();
?>
</body>