Here is a class I've began writing to add a new user to my database via a registration form. Everything has worked fine up until I try to do back-end checking to see if required values are not provided. Here is the code...
class User
{
//Basic Login variables
private $uName;
private $pWord;
private $cpWord;
private $shaPass;
//Account info vars
private $fName;
private $lName;
private $email;
private $confEmail;
private $mPhone;
private $hPhone;
//Trading info vars
private $trName;
private $phone; //Defaults to home phone, or cell, if no home number specified.
private $fax;
private $web;
//Home Address vars
private $h_addr1;
private $h_addr2;
private $h_city;
private $h_state;
private $h_zip;
//Postal Address vars
private $p_addr1;
private $p_addr2;
private $p_city;
private $p_state;
private $p_zip;
private $not_required_vars;
//Post name collection
private $post_coll;
//Error collection
private $errors;
public function User()
{
$this->not_required_vars = array('fax', 'web');
$this->post_coll = array('uName', 'pWord', 'cpWord', 'fName', 'lName', 'email', 'confEmail', 'dob', 'mPhone', 'hPhone', 'trName', 'phone', 'fax', 'web', 'h_addr1', 'h_addr2', 'h_city', 'h_state', 'h_zip', 'p_addr1', 'p_addr2', 'p_city', 'p_state', 'p_zip');
//print_r($_POST);
foreach($this->post_coll as $index)
{
if(isset($_POST[$index]) && (!empty($_POST[$index])))
{
//echo $index . '<br />';
$this->{$index} = $_POST[$index];
}else
{
foreach($this->not_required_vars as $not_req)
{
if($index != $not_req)
{
//echo $index . '<br />';
$this->errors = array();
$this->errors[] = array($index=>'This field is required');
break;
//header('location: '.$_SERVER['PHP_SELF'].'?page='. $_GET['page'] . '&error=1');
}
}
}
}
print_r($this->errors);
}
Let me try to save you time by explaining what the above does... I have the array of $_POST index strings called 'post_coll'. I then have my foreach loop through each string checking to see if that string isset in the $_POST array, if it's not, I check to see if it is a required value. If it is indeed a required value I want to create an associative array with the string (such as 'uName') as a key in the array with the value being something like "This field is required" that way when I redirect it back to my form I can getErrors from my class and display them where appropriate on the form.
Thanks in advance for any help.