Reputation: 439
In CodeIgniter, I designed a page, which there is a login form, and a simple home page. The validation I did client side validation only.
view login.php :-
<form name="login" id="login" action="<?php echo base_url() ?>login/success" onsubmit="return validatelogin()" method="post">
... HTML
<input type="text" id="username" name="username" />
<input type="password" id="passwrd" name="passwrd">
Javascript validation in the view page "login.php",
function validatelogin(){
var x=document.forms["login"]["username"].value;
var y=document.forms["login"]["passwrd"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
if (y==null || y=="")
{
alert("Password field must be filled out");
return false;
}
if(x!="monisha" && y!="monisha"){
alert("Username and Password incorrect");
return false;
}
return true;
}
Controller - login.php :-
class Login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->library('session');
}
function index(){
$this->load->view('login');
}
function success() {
$data = array('username' => "monisha");
$this->session->set_userdata($data);
redirect ('home');
}
}
I created the table "login", which is having the username and password fields.
I need to validate it using the database and the database query. Can anybody please assist me on what all changes I have to do in controller file, and the view page, to get this done.?
Upvotes: 0
Views: 56
Reputation: 7475
Learn form validation
in CI
first! http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
You need to change this accordingly.
In Controller:
$this->load->helper(array('form')); #no need to do this if already loaded
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required||is_unique[table_name.column_name]');
$this->form_validation->set_rules('passwrd', 'Password', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('view_name');
}
else
{
redirect('controller/function', 'refresh');
}
In view page:
<?php echo validation_errors(); ?>
Upvotes: 1