如何使用codeigniter从控制器为视图标签赋值

时间:2015-11-12 08:59:46

标签: php html codeigniter

我使用以下代码在html视图部分

中显示登录表单

<form action="/TestApp/index.php/my_controller/validate" method="POST" method="GET">
  Username: <input name="name" type="text"><br />
  Password: <input name="word" type="password"><br />
  <label name = "error"></label><br />
  <input type="submit" value="Login">
</form>

内部控制器我验证用户名和密码。以下代码行帮助我将文本框值转换为控制器。

$uName = $_POST['uname'];
$pWord = $_POST['pword'];

我需要从控制器分配/设置字符串值以标记错误。

我该怎么做?请帮帮我。

控制器

class My_controller extends CI_Controller {

public function login()
{
    $this->load->view('login_view');
}

    public function validate()
    {
        $this->load->model('login');



        if((isset($_POST['name'])) && (isset($_POST['word'])) && !empty($_POST['name']) && !empty($_POST['word']))
        {
            $uName = $_POST['name'];
            $pWord = $_POST['word'];

            if($this->model_users->checkUser($uName) == 1) {
                if($this->model_users->checkPassword($uName) == $pWord) {
                    echo 'Loggedin!';
                    $this->session->set_userdata('login_state', TRUE);
                } else {
//                        Incorrect password;
                    redirect( '/' );
                    $error = "Incorrect password!";
                }
            } else {
//                    Username does not exist;
                redirect( '/' );
                $error = "Username does not exist!";
            }
        } else {
//                Username or Password field is empty;
            redirect( '/' );
            $error = "Username or Password field is empty!";
        }
    }

}

4 个答案:

答案 0 :(得分:0)

带有验证的控制器部件代码

<?php

class Form extends CI_Controller {

	function index()
	{
		$this->load->helper(array('form', 'url'));

		$this->load->library('form_validation');

		$this->form_validation->set_rules('username', 'name', 'required');
		$this->form_validation->set_rules('password', 'word', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('myform');
		}
		else
		{
			$this->load->view('formsuccess');
		}
	}
}
?>

答案 1 :(得分:0)

行动应该是

action="<?php echo base_url() ?>my_controller/validate"
  

autoload.php加载网址助手以及

一种验证方法

$uName = $_POST['name']; # not uname
$pWord = $_POST['word']; # not uname

传递日期以查看

在控制器中

$data['error'] = "My Message here";

在视图中

if (!empty($error)) {
    ?>
    <label name ="error"></label>
    <?php
}
else{

}

答案 2 :(得分:0)

  

试试这个

视图中的

 <?php echo form_open('my_controller/validate', ['method'=>'post']) ?>
控制器内的

$uName = $this->input->post['name'];
$pWord = $this->input->post['word'];

答案 3 :(得分:0)

纠正这个,

在控制器中

import numpy as np
import cv2

#Get video name from user
#Ginen video name must be in quotes, e.g. "pirkagia.avi" or "plaque.avi"
video_name = input("Please give the video name including its extension. E.g. \"pirkagia.avi\":\n")

#Open the video file
cap = cv2.VideoCapture(video_name)

#Set frame_no in range 0.0-1.0
#In this example we have a video of 30 seconds having 25 frames per seconds, thus we have 750 frames.
#The examined frame must get a value from 0 to 749.
#For more info about the video flags see here: https://stackoverflow.com/questions/11420748/setting-camera-parameters-in-opencv-python
#Here we select the last frame as frame sequence=749. In case you want to select other frame change value 749.
#BE CAREFUL! Each video has different time length and frame rate. 
#So make sure that you have the right parameters for the right video!
time_length = 30.0
fps=25
frame_seq = 749
frame_no = (frame_seq /(time_length*fps))

#The first argument of cap.set(), number 2 defines that parameter for setting the frame selection.
#Number 2 defines flag CV_CAP_PROP_POS_FRAMES which is a 0-based index of the frame to be decoded/captured next.
#The second argument defines the frame number in range 0.0-1.0
cap.set(2,frame_no);

#Read the next frame from the video. If you set frame 749 above then the code will return the last frame.
ret, frame = cap.read()

#Set grayscale colorspace for the frame. 
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

#Cut the video extension to have the name of the video
my_video_name = video_name.split(".")[0]

#Display the resulting frame
cv2.imshow(my_video_name+' frame '+ str(frame_seq),gray)

#Set waitKey 
cv2.waitKey()

#Store this frame to an image
cv2.imwrite(my_video_name+'_frame_'+str(frame_seq)+'.jpg',gray)

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

在视图中

$uName = $this->input->post('name');
$pWord = $this->input->post('word');

if($uName == "" || $pWord == ""){// change the conditions if required.
    $error = 1;
} else {
    $error = 0;
}
$data['error'] = $error;
$this->load->view("view_name",$data);

如果您尝试使用重定向,请使用if($error) { echo "<label>Error has occurred</label>";// change error if required. } 。 在控制器中。

flashdata()

在视图中

$this->session->set_flashdata("error","error message here");