Bước 5: Tạo form input (ok) tạo thêm class AppModel, User extends AppModel && tạo file login.php
<?php
class Form {
private $model = 'Model';
protected $rules = null;
public $inputs = array();
public function input($name) {
$type = 'text';
foreach ($this->inputs as $field => $input) {
if ($field == $name) {
$type = $input['type'];
$style = isset($input['style']) ? ' style="' . $input['style'] . '"' : '';
switch ($type) {
case 'text':
$inputField = '<input type="text"' . $style . ' name="data[' . $this->model . '][' . $name . ']"' . ' value="' . (isset($this->data[$this->model][$name]) ? $this->data[$this->model][$name] : '') . '" />';
break;
case 'password':
$inputField = '<input type="password"' . $style . ' name="data[' . $this->model . '][' . $name . ']"' . ' value="' . (isset($this->data[$this->model][$name]) ? $this->data[$this->model][$name] : '') . '" />';
break;
}
}
}
echo $inputField;
}
public function setRules($rules) {
if (!empty($rules)) {
$this->rules = $rules;
foreach ($rules as $field => $rule) {
if (isset($rule['form'])) {
$this->inputs[$field] = $rule['form'];
}
}
}
}
}
?>
<?php
if(!class_exists('Form')) require_once 'Form.php';
class AppModel {
public $form = null;
protected $rules = null;
public function __construct() {
$this->form = new Form();
$this->form->setRules($this->rules);
}
};
?>
<?php
require_once "AppModel.php";
class User extends AppModel {
protected $rules = array(
"email" => array(
"form" => array(
"type" => "text"
),
"notEmpty" => array(
"rule" => "notEmpty",
"message" => MSG_ERR_NOTEMPTY
),
"isEmail" => array(
"rule" => "email",
"message" => MSG_ERR_EMAIL
)
),
"password" => array(
"form" => array(
"type" => "password"
),
"notEmpty" => array(
"rule" => "notEmpty",
"message" => MSG_ERR_NOTEMPTY
)
),
"fullname" => array(
"form" => array(
"type" => "text"
),
"notEmpty" => array(
"rule" => "notEmpty",
"message" => MSG_ERR_NOTEMPTY
)
),
"address" => array(
"form" => array(
"type" => "textarea"
),
"notEmpty" => array(
"rule" => "notEmpty",
"message" => MSG_ERR_NOTEMPTY
)
)
);
public function __construct() {
parent::__construct();
}
}
<?php
require_once 'const.php';
require_once 'User.php';
$user = new User();
?>
<!DOCTYPE html>
<title>User Login</title>
<link href="css/reset.css" rel="stylesheet" type="text/css" media="all">
<link href="css/main.css" rel="stylesheet" type="text/css" media="all">
<link href="css/form.css" rel="stylesheet" type="text/css" media="all">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
</head>
<body>
<header>
<div class="logo">
<img src="images/film.png" width="45" />
<div class="title">Platinum Cineplex</div>
</div>
<nav>
<ul>
<li><a href="#"><img src="images/logout.png" width="25">Logout</a></li>
</ul>
</nav>
</header>
<nav>
<ul id="dropmenu">
<li>
<a href="#">Movie</a>
</li>
</ul>
</nav>
<div class="heading">User Login</div>
<form action="" class="form" method="post">
<section>
<dl>
<dt>Email</dt>
<dd>
<?php echo $user->form->input('email'); ?>
</dd>
</dl>
</section>
<section>
<dl>
<dt>Password</dt>
<dd>
<?php echo $user->form->input('password'); ?>
</dd>
</dl>
</section>
<section>
<dl>
<dd>
<input type="submit" name="submit" value="Login"><br><br>
<a href="http://localhost/testoop/login.php">Register</a>
</dd>
</dl>
</section>
</form>
</div>
</body>
</html>
PreviousBước 4+: Cách đổi mảng Model thành mảng User (ok)NextBước 5+ Bước tạo hằng số kết nối Database (ok)
Last updated