PHP CodeIgniter 3 Validation Example Tutorial From Scratch

Today, We want to share with you PHP CodeIgniter 3 Validation Example Tutorial From Scratch.In this post we will show you CodeIgniter and Ajax using jQuery, hear for Php CodeIgniter Server Side Form Validation Example we will give you demo and example for implement.In this post, we will learn about Ajax contact us form validation in Codeigniter 3 using javascript jQuery with an example.

PHP CodeIgniter 3 Validation Example Tutorial From Scratch

There are the Following The simple About PHP CodeIgniter 3 Validation Example Tutorial From Scratch Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP jQuery Ajax CodeIgniter Form Validation, so the some major files and Directory structures for this example is following below.

  • PHP jQuery Ajax CodeIgniter Form Validation
  • FormCiValidate.php
  • ajax_form_validate.php

Step 1: Include Codeigniter 3 Routes

Include routes to handle ajax request

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['simple-ajax-frm-validate'] = "FormCiValidate";
$route['simple-ajax-frm-validate/post']['post'] = "FormCiValidate/validateForm";

Step 2: Make FormCiValidate simple Controller Class

application/controllers/FormCiValidate.php

load->library('form_validation');
      $this->load->library('session');
   }

   public function index()
   {
      $this->load->view('ajax_form_validate');
   }

   public function validateForm()
   {
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('comments', 'Comment', 'required');


        if ($this->form_validation->run() == FALSE){
            $errors = validation_errors();
            echo json_encode(['error'=>$errors]);
        }else{
           echo json_encode(['success'=>'Form submitted successfully.']);
        }
    }
}

Step3: Make HTML Interface View File

ajax_form_validate.php

   
    <title>CodeIgniter 3 Form Submission Using jQuery - pakainfo.com</title>
     
    
  
   
  <div class="pakainfo container">
    <div class="row">
      <div class="col-sm-8 col-sm-offset-2">
        <div class="alert alert-info">
        </div>
		<h1>PHP jQuery Ajax CodeIgniter Form Validation</h1>
       
        <div class="gst form-group pakainfo">
          <label>Name:</label>
          
        </div>

        <div class="gst form-group pakainfo">
          <strong>Email:</strong>
          
        </div>

        <div class="gst form-group pakainfo">
          <strong>Comment:</strong>
          <textarea class="lst form-control" name="comments"></textarea>
        </div>

        <div class="gst form-group pakainfo">
          <button class="btn btn-success btn-block frm-submit">Submit</button>
        </div>
      </form>
    </div>
    </div>
  </div>
   


   
    $(document).ready(function() {
      $(".frm-submit").click(function(e){
        e.preventDefault();
        var name = $("input[name='name']").val();
        var email = $("input[name='email']").val();
        var comments = $("textarea[name='comments']").val();

        console.log($(this).closest('form').attr('action'));
          $.ajax({
              url: $(this).closest('form').attr('action'),
              type:$(this).closest('form').attr('method'),
              dataType: "json",
              data: {name:name, email:email, comments:comments},
              success: function(data) {
                  if($.isEmptyObject(data.error)){
                    $(".alert-info").css('display','none');
                    alert(data.success);
                  }else{
                    $(".alert-info").css('display','block');
                    $(".alert-info").html(data.error);
                  }
              }
          });


      }); 


  });

   

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read This 👉   Laravel 6 CRUD Resource Route and Controller Tutorial

Related Search: PHP jQuery Ajax CodeIgniter Form Validation, Ajax contact us form validation in Codeigniter 3 using javascript jQuery,form validation in codeigniter using jquery,simple contact us form validation using jquery in codeigniter 3 with example

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP CodeIgniter 3 Validation Example Tutorial From Scratch.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.