Form Validation in CodeIgniter by Simple Steps with Send Email Functionality

Form Validation is very important in a website to get rid of spam enquires or viruses. Spammers try to execute the malicious scripts via form inputs and robots fill the spam enquires; these both harm our websites, so get rid of these types of problems we use form validation.

CodeIgniter provides form validation library which is easy to integrate, easy to use. There is no need to write hard code like in core php.Let’s see the example of form validation with send email by the following steps:

Step1: Create file contact.php in view and write the following code:

 <title>Contact Us</title>
 <?php echo validation_errors(); ?>
 <?php echo form_open('form'); ?>
 <input type="text" name="name" value="" size="50" />

<?php echo form_error('name'); ?>   <h5>Mobile</h5> <input type="text" name="mobile" value=""  />

<?php echo form_error('mobile'); ?>   <h5>Email Address</h5> <input type="text" name="email" value="" size="50" />

<?php echo form_error('email'); ?>
<h5>Message</h5> <textarea name="message" > </textarea>     <div><input type="submit" value="Submit" /></div>   </form>   </body> </html>  

Step2: Create a new Controller Form.php and write the following code:

 class Form extends CI_Controller { 
         public function index() 
                 if ($this->form_validation->run() == TRUE) 
                         $message = '<html><body>'; 
 $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; 
 $message .= "<tr style='background: #eee;'><td colspan='2'><strong>Website Enquiry</strong> </td></tr>"; 
 $message .= "<tr><td><strong>Name:</strong> </td><td>" . $_POST['name'] . "</td></tr>"; 
 $message .= "<tr><td><strong>Mobile:</strong> </td><td>" . $_POST['mobile'] . "</td></tr>"; 
 $message .= "<tr><td><strong>Email:</strong> </td><td>" . $_POST['email'] . "</td></tr>"; 
 $message .= "<tr><td><strong>Message:</strong> </td><td>" . $_POST['message'] . "</td></tr>"; 
 $message .= "</table>"; 
 $message .= "</body></html>"; 
                                     $this->email->subject('Website Enquiry'); 

Here in this step2, we have used the required validation of the form validation library.

  • the first parameter is form input name,
  • the second parameter will be shown in error
  • third is the validation rule.

Categories: web development codeigniter

Trending Courses


Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Advance Digital Marketing Expert Course

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 25 Days

Crash : 15 Days


Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 6 Months

Fastrack : 4 Months

Crash : 2 Months

Web Expert with Python

Regular : 12 Months

Fastrack : 6 Months

Crash : 3 Months

Related Blogs

Request For Demo