本集讲一个小的login例子,依次来演示form的使用。感觉上ci3与ci4在form方面的差别不大。本节还有错误有待修正。
1.引入form包
config/autoload.php
$autoload['helper'] = array('url','form');
2.Form提交数据
views/Login_new.php
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Login System</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css" integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">
</head>
<body>
<?php
$attributes = array('class'=>'form-horizontal','method'=>'get');
$in_attributes = array('class'=>'form-control','name'=>'user');
$in_pass_attributes = array('class'=>'form-control','name'=>'password','placeholder'=>'Enter your password','type'=>'password');
$in_btn_attributes = array('class'=>'btn btn-success','name'=>'submit','type'=>'submit','value'=>'Submit');
echo '<div class="container">';
echo form_open('Login_con/process',$attributes);
echo form_label('username');
echo form_input($in_attributes);
echo form_label('Password');
echo form_input($in_pass_attributes);
echo form_input($in_btn_attributes);
echo form_close();
echo '</div>';
?>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</body>
</html>
controllers/Login_con.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_con extends CI_Controller {
public function index()
{
$this->load->view('Login_view');
}
public function process()
{
echo $_GET['user'];
echo $this->input->get('password');
}
}
可以试试
3.Form输入数据格式检查
controller/Login_con.php 这种写法其实不是最官方的写法, 最官方的validation_errors失败了,所以才换了这个写法
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_con extends CI_Controller {
public function index()
{
$this->load->view('Login_view');
}
public function process()
{
$this->form_validation->set_rules('user','Joe','min_length[3]|max_length[8]|required'
);
// $this->form_validation->set_rules('password','parys','min_length[4]|max_length[8]|required');
if($this->form_validation->run() == FALSE){
echo 'there are some error';
// echo validation_errors();
$val_error = array(
'errors' =>" Data does not meet requirement ",
);
//set_flashdata 只存在于一个request,下一个request就没有了
$this->session->set_flashdata($val_error);
redirect('Login_con');
}else{
echo 'hello',$this->input->get('user');
}
}
}
views/Login_view.php
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Login System</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css" integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">
</head>
<body>
<?php
if($this->session->flashdata('errors')){
echo $this->session->flashdata('errors');
}else{
echo 'There is no error';
}
$attributes = array('class'=>'form-horizontal','method'=>'get');
$in_attributes = array('class'=>'form-control','name'=>'user');
$in_pass_attributes = array('class'=>'form-control','name'=>'password','placeholder'=>'Enter your password','type'=>'password');
$in_btn_attributes = array('class'=>'btn btn-success','name'=>'submit','type'=>'submit','value'=>'Submit');
echo '<div class="container">';
echo form_open('Login_con/process',$attributes);
echo form_label('username');
echo form_input($in_attributes);
echo form_label('Password');
echo form_input($in_pass_attributes);
echo form_input($in_btn_attributes);
echo form_close();
echo '</div>';
?>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</body>
</html>
3.进一步Model数据
略过