2
votes

I use the following code to display a login form in html view part

<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>

inside controller I validate username and password. The following lines of code help me to get textbox values into controller.

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

I need to assign/set string value to label error from controller.

How can I do that? please help me.

Controller

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

4 Answers

0
votes

The controller part code with validation

<?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');
		}
	}
}
?>
0
votes

action should be

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

In autoload.php load url helper as well

an in validate method

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

To pass date to view

In Controller

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

In View

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

}
0
votes

try this

inside the view

 <?php echo form_open('my_controller/validate', ['method'=>'post']) ?>

inside the controller

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

Correct this,

In controller

$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);

In view

if($error) { 
    echo "<label>Error has occurred</label>";// change error if required.
}

If you are trying with redirect, use flashdata(). In controller.

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

In view

if($this->session->flashdata("error"){
    // echo label here
}