How to do form validation in codeigniter – onlinecode

How to do form validation in codeigniter – onlinecode

In this post we will give you information about How to do form validation in codeigniter – onlinecode. Hear we will give you detail about How to do form validation in codeigniter – onlinecodeAnd how to use it also give you demo for it if it is necessary.

Today we are going to learn more about how to create server-side form validation using CodeIgniter framework.

Whenever creating Codeigniter validation at that time load “form_validation” and “session” library and also load helpers such as form, URL, and HTML.

if the user fills all form data then click on button and form_validation is true then data will be inserted into database otherwise given the error message.

See below code.

Create controller and put below method

public function add()
{
    $this->load->library(array('form_validation','session'));
    $this->load->helper(array('url','html','form')); 
	if($this->input->post('btnadd'))
	{
	  $this->form_validation->set_rules('txtCategoryName', 'Category Name', 'trim|required');

            if ($this->form_validation->run() == TRUE)
            {
		$arrData['first_name'] = $this->input->post('txtFname');
		$arrData['last_name'] = $this->input->post('txtLname');
		$arrData['address'] = $this->input->post('txtAddress');
		$arrData['email'] = $this->input->post('txtEmail');
		$arrData['mobile'] = $this->input->post('txtMobile');

		$insert= $this->register_model->insert($arrData);
		if($insert)
		{
		 redirect('register');
		}
	    }	
	}   
	$this->load->view('register/add');
}

Create view file of add.php

<form method="post" name="frmAdd">
        <table align="center">
            <tr>
                <td colspan="2" align="center">Add Record</td>
            </tr>

            <tr>
                <td>First Name</td>
                <td>
		   <input type="text" name="txtFname"> 
                   <span ><?php echo form_error('txtFname'); ?></span>  
	        </td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td>
		  <input type="text" name="txtLname">
		  <span ><?php echo form_error('txtLname'); ?></span>					
		</td>
            </tr>
            <tr>
                <td>Address</td>
                <td><pre name="txtAddress" rows="4" cols="16"></pre> </td>
            </tr>
            <tr>
                <td>Email</td>
                <td>
		  <input type="text" name="txtEmail">
		  <span ><?php echo form_error('txtEmail'); ?></span>					
		</td>
            </tr>
            <tr>
                <td>Mobile</td>
                <td>
		  <input type="text" name="txtMobile">
		  <span ><?php echo form_error('txtMobile'); ?></span>  
	       </td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Add" name="btnadd"> </td>
            </tr>
        </table>
</form>

Please follow and like us:

Hope this code and post will helped you for implement How to do form validation in codeigniter – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

22  +    =  24

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US