【发布时间】:2015-11-12 07:06:44
【问题描述】:
我正忙于学习 PHP,并且一直在按照教程创建一个基本站点,您可以在其中注册并拥有用户帐户/更改密码/更新信息等等
我参与了您更改密码的工作(我在这个 Video 中大约是 6:10),我已经陷入困境。就我而言,我已经完成了他在教程中所做的一切,但是当我提交表单时,出现以下错误。请对我放轻松,因为我确定我遗漏了一个“,”或拼写错误,但对于我的生活,我找不到它。 (注意:当我提交表单时,我确实输入了我当前的密码,但我收到验证错误,说我没有。)
(更新:将这篇文章标记为重复并将我指向另一篇文章并不是很有帮助。正如我试图在上面强调的那样,我只是在学习 PHP,因此无法从帖子中找出解决方案,并且不知何故把它和我的问题联系起来。)
注意:未定义索引:第 15 行 C:\wamp\www\Movrate\classes\Validate.php 中的 password_current
注意:未定义索引:第 15 行 C:\wamp\www\Movrate\classes\Validate.php 中的 password_current
注意:未定义索引:第 15 行 C:\wamp\www\Movrate\classes\Validate.php 中的 password_new
注意:未定义索引:第 15 行 C:\wamp\www\Movrate\classes\Validate.php 中的 password_new
注意:未定义索引:第 15 行 C:\wamp\www\Movrate\classes\Validate.php 中的 password_new_again
注意:未定义索引:第 15 行 C:\wamp\www\Movrate\classes\Validate.php 中的 password_new_again
注意:未定义索引:第 15 行 C:\wamp\www\Movrate\classes\Validate.php 中的 password_new_again
password_current 是必需的
password_new 是必需的
password_new_again 是必需的
这是我更新密码页面的代码:
<?php
require_once 'core/init.php';
$user = new User();
if(!$user->isLoggedIn()) {
Redirect::to('index.php');
}
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'password_current' => array(
'required' => true,
'min' => 6
),
'password_new' => array(
'required' => true,
'min' => 6
),
'password_new_again' => array(
'required' => true,
'min' => 6,
'matches' => 'password_new'
)
));
if($validation->passed()) {
// change of password
} else {
foreach($validation->errors() as $error) {
echo $error, '<br>';
}
}
}
}
?>
<form action="" method="post">
<div class="field">
<lable for="password_current">Current password</label>
<input type="password" name="passsword_current" id="password_current">
</div>
<div class="field">
<lable for="password_new">New password</label>
<input type="password" name="passsword_new" id="password_new">
</div>
<div class="field">
<lable for="password_new_again">New password again</label>
<input type="password" name="passsword_new_again" id="password_new_again">
</div>
<input type="submit" value="Change">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</form>
这是我的验证类的代码:
<?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]);
$item = escape($item);
if($rule === 'required' && empty($value)) {
$this->addError("{$item} is required");
} else if(!empty($value)) {
switch($rule) {
case 'min':
if(strlen($value) < $rule_value) {
$this->addError("{$item} must be a minimum of {$rule_value} characters.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError("{$item} must be a maximum of {$rule_value} characters.");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("{$rule_value} must match {$item}");
}
break;
case 'unique':
$check = $this->_db->get($rule_value, array($item, '=', $value));
if($check->count()) {
$this->addError("{$item} already exists.");
}
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;
}
}
【问题讨论】:
-
检查
$_POST中是否存在键password_current、password_new、password_new_again
标签: php forms validation undefined-index