我有一个模式登录表单,要求输入电子邮件和密码。我也有一个“ Validate.php”类,它可以对表单进行验证。我还将错误存储为唯一数组。现在,我想做的是我想在模式形式内进行验证。正常情况下,我设法以模式形式显示错误消息,但页面重新加载后,然后单击“登录”按钮后,它显示了消息。我想在重新加载页面之前显示错误消息。
我知道我应该为此使用AJAX,但实际上我不知道该怎么做。我知道下面有很多代码,我只是想知道如何更好地使用AJAX。
验证类:
<?php
class Validate {
private $_passed = false, $_errors = array(), $_db = null;
public function __construct($name = '') {
$this->_db = DB::getInstance();
echo $name;
}
public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$value = trim($source[$item]);
$item = escape($item);
// $source = $_POST[]
switch($item) {
case 'user_nickname':
$fieldname = 'Kullanıcı Adınız';
break;
case 'user_email':
$fieldname = 'E-posta';
break;
case 'user_password':
$fieldname = 'Şifre';
break;
case 'user_password_again':
$fieldname = 'Şifre (Tekrar)';
break;
default:
$fieldname = '';
break;
}
// Now, check for anything
// 1. Check for required inputs are not empty.
if($rule == 'required' && empty($value)) {
$this->addError("Lütfen tüm alanları doldurun.");
} else if(!empty($value)) {
switch($rule) {
case 'min':
if(strlen($value) < $rule_value) {
$this->addError("{$fieldname} alanı en az {$rule_value} uzunluğunda olmalı.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError("{$fieldname} alanı en fazla {$rule_value} uzunluğunda olmalı.");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("Şifreler uyuşmuyor.");
}
break;
case 'unique':
// handle it later
break;
}
}
}
}
if(empty($this->_errors)) {
$this->_passed = true;
}
return $this;
}
private function addError($error) {
$this->_errors[] = $error;
}
public function errors() {
return array_unique($this->_errors);
}
public function passed() {
return $this->_passed;
}
}
登录表格:
<!-- Login Modal -->
<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="loginModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title m-auto" id="loginModalLabel">HESAP OLUŞTUR</h5>
</div>
<div class="modal-body">
<?php if($msg != '') :?>
<div class="alert alert-danger"><span class="oi oi-info"></span> <?php echo $msg ?></div>
<?php endif ?>
<form action="" method="POST" id="loginForm">
<div class="form-group">
<input type="email" class="form-control" name="user_email" value="<?php echo escape(Input::get('user_email')); ?>" id="email" aria-describedby="email" placeholder="E-posta Adresiniz">
</div>
<div class="form-group">
<input type="password" class="form-control" name="user_password" id="password" placeholder="Şifreniz">
</div>
<div class="form-group">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="remember" id="remember" checked="">
<label class="custom-control-label" for="remember">Beni Hatırla</label>
</div>
</div>
<input type="hidden" name="loginToken" value="<?php echo Token::generate('login_token'); ?>">
<input type="hidden" name="currentPage" value="<?php echo $_SERVER['REQUEST_URI']; ?>">
<button type="submit" name="submit_login" class="btn btn-primary btn-block submit_login">Giriş Yap</button>
</form>
</div><!-- .modal-body -->
<div class="modal-footer">
<div class="container">
<div class="row">
<div class="col-6">
<a href="#" class="btn btn-light">Şifremi Unuttum</a>
</div>
<div class="col-6">
<a href="#" class="btn btn-light" data-dismiss="modal" data-toggle="modal" data-target="#registerModal" target="_blank">Hesap Oluştur</a>
</div>
</div><!-- .modal-footer > .row -->
</div><!-- .modal-footer > .container -->
</div><!-- .modal-footer -->
</div><!-- .modal-content -->
</div><!-- .modal-dialog -->
</div><!-- .modal -->
最后,我对注册和登录的验证:
<?php
$msg = '';
if(Input::exists()) {
if(Token::check(Input::get('registerToken'), 'register_token')) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'user_nickname' => array(
'required' => true,
'min' => 2,
'max' => 20,
'unique' => 'users'
),
'user_password' => array(
'required' => true,
'min' => 6
),
'password_again' => array(
'required' => true,
'matches' => 'user_password'
),
'user_email' => array(
'required' => true,
'unique' => 'users'
)
));
if($validation->passed()) {
$user = new User();
// echo $salt = Hash::salt(32);
// die();
$salt = Hash::salt(32);
try {
$user->create(array(
'user_nickname' => Input::get('user_nickname'),
'user_password' => Hash::make(Input::get('user_password'), $salt),
'user_email' => Input::get('user_email'),
'user_salt' => $salt,
'user_group' => 1,
'created_at' => date('Y-m-d H:i:s')
));
} catch(Exception $e) {
die($e->getMessage());
}
$remember = (Input::get('remember') === 'on') ? true : false;
$login = $user->login(Input::get('user_email'), Input::get('user_password'), $remember);
$currentPage = Input::get('currentPage');
Redirect::to($currentPage, 0);
} else {
foreach($validation->errors() as $error) {
$msg = $error;
}
} // Token::check(Input::get('registerToken'))
}
if(Token::check(Input::get('loginToken'), 'login_token')) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'user_email' => array(
'required' => true
),
'user_password' => array(
'required' => true
)
));
// if validation is okay.
if($validation->passed()) {
// create user object
$user = new User();
// get remember value
$remember = (Input::get('remember') === 'on') ? true : false;
// login user
$login = $user->login(Input::get('user_email'), Input::get('user_password'), $remember);
if($login) {
// update last login
$currentTime = date('Y-m-d H:i:s');
try {
$user->update(array(
'user_last_login' => $currentTime
), $user->data()->id);
} catch(Exception $e) {
die($e->getMessage());
}
$currentPage = Input::get('currentPage');
Redirect::to($currentPage, 0);
} else {
$msg = 'E-posta veya kullanıcı adınız hatalı.';
}
} else {
foreach($validation->errors() as $error) {
$msg = $error;
}
} // Token::check(Input::get('loginToken'))
}
}