Codeigniter 3.1.11笔记(3)

本集讲一个小的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数据
略过

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值