This question already has an answer here:
I'm getting a really weird error from a file that I haven't changed at all during both these file creations. It was working fine and only started happening when I added the match for the passwords - then I deleted it to see if the error was resolved and the error was still there.
Also it appears in some of my text boxes with BR tags in between the words. however the script still does what its supposed to if I delete the errors from the text boxes and add some test data.
The guy in the tutorial I'm watching does not get these errors.
Notice: Undefined index: username in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/Input.php on line 23
Notice: Undefined index: name in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/Input.php on line 23
register.php
<?php
require_once 'core/init.php';
if(Input::exists()) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'username' => array(
'required' => true,
'min' => 6,
'max' => 20,
'unique' => 'users'
),
'password' => array(
'required' => true,
'min' => 6
),
'password_again' => array(
'required' => true,
'matches' => 'password'
),
'name' => array(
'required' => true,
'min' => 2,
'max' => 50
)
));
if($validation->passed()) {
echo "passed";
} else {
print_r($validation->errors());
}
}
?>
<form action ="" method= "post">
<div class="field">
<label for= "username">Username</label>
<input type= "text" name= "username" id ="username" value="<?php echo escape(Input::get('username')); ?>" autocomplete="off">
</div>
<div class="field">
<label for="password">Choose a password</label>
<input type="password" name="password" id="password">
<div class="field">
<label for="password_again">Confirm password</label>
<input type="password" name="password_again" id="password_again">
<div class="field">
<label for= "name">Name</label>
<input type= "text" name= "name" value="<?php echo escape(Input::get('name')); ?>" id ="name">
</div>
<input type="submit" value="Register">
</form>
Validate.php
<?php
class Validate{
private $_passed = false,
$_errors = array(),
$_db = null;
public function __construct() {
$this->_db = DB::getInstance();
}
public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$value = trim($source[$item]);
if($rule === 'required' && empty($value)) {
$this->addError("{$item} is required");
} else if(!empty($value)) {
switch($rule) {
case 'max':
if (strlen($value) > $rule_value) {
$this->addError("{$item} must be a maximum of {$rule_value} charcters");
}
break;
case 'min':
if (strlen($value) < $rule_value) {
$this->addError("{$item} must be a minimum of {$rule_value} characters");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("{$rule_value} must match {$item}");
}
break;
case 'unique':
break;
}
}
}
}
if (empty($this->_errors)) {
$this->_passed = true;
}
return $this;
}
private function addError($error) {
$this->_errors[] = $error;
}
public function errors() {
return $this->_errors;
}
public function passed(){
return $this->_passed;
}
}
Input.php
<?php
class Input {
public static function exists($type = 'post'){
switch ($type) {
case 'post':
return (!empty($_POST)) ? true : false;
break;
case 'get':
return (!empty($_GET)) ? true : false;
break;
default:
return false;
break;
}
}
public static function get($item) {
if(isset($_POST[$item])) {
return $_POST[$item];
} elseif(isset($_GET)) {
return $_GET[$item];
}
return '';
}
}
</div>