Create a form in CodeIgniter

Hello guys..lets see the syntax flow of form in code igniter now..

Goto your viewname.php:

<?php echo form_open(); // for method type post and action=”somepage.php”

echo form_input(); // for input type

echo form_password(); // for password type

echo form_submit(); // for submit functionality

form_open: form_open(‘main/login_validation’); //controller/function

Now create function in maion controller like

public function login_validation()

{

$this->load->library(‘form_validation’);
$this->form_validation->set_rules(’email’,’Email’,’required|trim|xss_clean|callback_validate_credentials’);//setting rule when for email box to be required/trim/xss_clean prevent cross side scripting/callback will tell compiler to check for validate_credentials function in controller.
$this->form_validation->set_rules(‘password’,’Password’,’requried|md5′);//similarly setting rule for pwd & set to md5 to store in database table
if($this->form_validation->run()){ //if validation succeeds
redirect(‘main/members’); // redirect to members page
}
else
{
$this->load->view(‘login’); //else redirect to login page
}

}

public function validate_credentials() // we will load models here to check with database
{
$this->load->models(‘model_users’);
if($this->model_users->can_log_in()){ // can_log_in function shud create in model_users.php to check logged in or not
return true;
}
else
{
$this_form_validation->set_message(‘validate_credentials’,’Incorrect Username/Password’); //if failed in validation then display incorrect message for form_validation thru validate_credentials
return false;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s