Form Validation in CodeIgniter by Simple Steps with Send Email Functionality

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:

<html>
 <head>
 <title>Contact Us</title>
 </head>
 <body>
  
 <?php echo validation_errors(); ?>
   
 <?php echo form_open('form'); ?>
  
 <h5>Name</h5>
 <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:

<?php 
 class Form extends CI_Controller { 
   
         public function index() 
         { 
                 $this->load->library('form_validation'); 
             $this->form_validation->set_rules('name','Name','trim|required|min_length[4]|max_length[20]'); 
 $this->form_validation->set_rules('email','Email','required|valid_email'); 
 $this->form_validation->set_rules('mobile','phone','required|numeric|exact_length[10]'); 
   
                 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->load->library('email'); 
                                     $this->email->set_newline("\r\n"); 
             $this->email->set_mailtype("html"); 
                                     $this->email->from('Enquiry-HomePage'); 
                                     $this->email->to('your-email'); 
                                     $this->email->subject('Website Enquiry'); 
                                     $this->email->message($message); 
   
                           if($this->email->send()) 
                           {                          
  redirect('thankyou'); 
 } 
                 } 
                         $this->load->view('contact'); 
         } 
 } 
   
 

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.