Ajax form validation in codeigniter

codeigniter form validation
modal form validation using jquery ajax in codeigniter
registration form validation using ajax in php
codeigniter login form validation example
ajax in codeigniter with forms
codeigniter validation before submit
codeigniter registration form with validation
form validation not working in codeigniter

hellp guys,

I've been working on ajax recently, and I have a problem in using it with codeigniter form validation library. I used the example that tool generate in the function http://formtorch.geekhut.org/. Now, ajax works perfectly and return data correctly when I use json_encode() function with dummy data, but validation in the example uses validation library instead of form_validation library, which seems to be older version.

For that, validation didn't work with ajax in that example, specifically $this->form_validation->run() function makes ajax return no result even if I echo dummy data using json_encode() in the beginning of create_course().

so what's wrong with validation with ajax, and explain to me how data sent by ajax received by the controller.

so this is my code:

    function create_course()
        $this->form_validation->set_rules('course_code', 'course_code', 'trim|xss_clean|required');
        $this->form_validation->set_rules('name', 'name', 'xss_clean|required');
        // .. etc           
        if ($this->form_validation->run()) {            
            // validation ok
            $data['course_code'] = $this->form_validation->set_value('course_code');
            $data['name'] = $this->form_validation->set_value('name');
            // ... etc
            if ($this->models_facade->create_course($user_id,$data))    {   // success
                    $data = array( 'profile_change' => $this->lang->line('profile_change'));
            } else {                    // fail 
                    $data = array( 'profile_change_error' => $this->lang->line('profile_change_error'));
            $data = array(
                    'course_code' => $this->form_validation->course_code_error, 
                    'name' => $this->form_validation->name_error
        echo json_encode($data);

and this is the Jquery Ajax function

        var course_code = $("#course_code").val(); 
        var name = $("#name").val(); 
        // etc          
        $.post("<?php echo base_url() ?>home/create_course",  course_code:course_code, name:name},
   return false;


What version of Codeigniter are you using? Did you remember to load the validation library in your construct?


Ajax Form Validation in Codeigniter with JSON, If you not know how to validate form data by using Codeigniter Form validation libraray by using Ajax and receive response in JSON data type. Here, CodeIgniter 4 tutorial, we will create a contact form and submit contact form using Ajax and validate the contact form before sending the form data to the server in CodeIgniter 4. And this contact form will send data to the server without refreshing the whole page and Also, the form will insert the data into the database in CodeIgniter 4 framework.

Rather than printing out via "this->form_validation->xxxx_error" you can utilize Form Helper method "form_error()" to call the error messages.

So you can do something like..

$data = array(
                    'course_code' => form_error('course_code'), 
                    'name' => form_error('name')

Codeigniter Form Validation using Ajax jQuery JSON, codeigniter 3 ajax validation errors, form validation in codeigniter using jquery ajax, form submit in codeigniter using ajax, codeigniter ajax form  Which has been send ajax request to Codeigniter method which will perform validation rules on form data by using Form Validation library. If there is any validation error occur then by using form_error () method it will fetch validation error and store under array and send to back to ajax request in JSON format.

You might also consider setting the output content type header for JSON data.

echo json_encode($data);

Codeigniter Ajax Form Validation Example, we will create contact us form and form submit using ajax in codeigniter 3 app. we will use validation library of codeigniter and print error  We will implement server-side validation rules in codeigniter application on form submission using ajax. It is very simple to use ajax validation with codeigniter. Here i explain every thing step by step for implement ajax validation.

If you're making an ajax request you can use the validation_errors(). When the validation run it'll populate the array of error messages.

Here an exemple :

// Set your rules
$this->form_validation->set_rules('course_code', 'course_code', 'trim|xss_clean|required');
$this->form_validation->set_rules('name', 'name', 'xss_clean|required');

if ($this->form_validation->run()) {  
  //happy happy time
else { 
  //well now i'm sad...

  //Important to turn that off if it's on


  echo json_encode(array(
      'error_msg' => validation_errors(),

And then on your client-side you can use the response like that :

error:function(data) {

Hope i helped even if i'm 2 year late.

P.S Sorry for my broken english.

PHP Codeigniter 3 - JQuery Ajax Form Validation Example, Submit AJAX form in Codeigniter with server side form validation and data insert into database. Controller. application/controllers/Pages.php. <? Today, I would like to share with you how to create jquery ajax form validation in codeigniter 3 app. Here i will create simple form and make it server side validation using form_validation library. form_validation will help to easily set custom rule, success, error messages. Validation is very important part of every project.

****//view-path [application/views/myviews/myview2.php]****

   <script src="<?php echo base_url('/jquery-1.9.1.min.js');?>"></script>
$(document).ready(function() {

    $("#frm").on('submit',(function(e) {
            url: $('#frm').attr('action'),
            type: "POST",
            data:  new FormData(this),
            contentType: false,
            cache: false,
            success: function(data){
                    data = JSON.parse(data);
                    if(data.st == 0)
                        $( ".error-message" ).remove();
                        var data1    = JSON.parse(data.msg); 
                        $('form :input').each(function(){                            
                                        var elementName = $(this).attr('name');        
                                        var message = data1[elementName];
                                        var element = $('<div>' + message + '</div>')
                                                            'class' : 'error-message'
                                                        .css ({
                                                            display: 'none'

                    if(data.st == 1)
                      $( ".error-message" ).remove();
            error: function(){}             

<?php echo form_open_multipart('ajaxcontroller/index', array('id'=>'frm')); ?>

<div id="validation-error"></div>

<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />

<input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" />

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

<h5>Profile Pic</h5>
<input type="file" name="image[]" value="" multiple=""/>

<div><input type="submit" value="Submit" /></div>



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

class Ajaxcontroller extends CI_Controller {

    function __construct()


    function index()
            $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
            $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
            $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email');

            if (empty($_FILES['image']['name'][0])) {
                $this->form_validation->set_rules('image[]', 'File', 'required'); 

            if ($this->form_validation->run() == FALSE)
                $errors = $this->form_validation->error_array(); //function in library : My_Form_validation            
                echo json_encode(array('st'=>0, 'msg' => json_encode($errors)));

                $username = $this->input->post('username');
                $password = $this->input->post('password');
                $email = $this->input->post('email');

                if(is_array($_FILES)) {
                    foreach ($_FILES['image']['name'] as $name => $value){
                        if(is_uploaded_file($_FILES['image']['tmp_name'][$name])) {
                            $sourcePath = $_FILES['image']['tmp_name'][$name];
                            $targetPath = "images/".$_FILES['image']['name'][$name];
                            if(move_uploaded_file($sourcePath,$targetPath)) {


                echo json_encode(array('st'=>1, 'msg' => 'Successfully Submiited'));


**//library file -- path will be [application/libraries/MY_Form_validation.php]**
 * Enter description here ...
 * @return CI_Controller
class MY_Form_validation extends CI_Form_validation
    public function error_array()
        return $this->_error_array;

Submit form using AJAX in CodeIgniter with form validation, I am going to use Codeigniter Form Validation class to validate contact us form, If the error exists then It will display the proper validation errors. I will use jQuery to​  Jquery submit form ajax in Codeigniter without refresh whole page with validate form data on client side. We will discuss how to submit a form using ajax without refresh or reload whole page, we will use jquery submit handler with jquery validation rules for ajax form submission.

Ajax contact us form validation in Codeigniter 3 using javascript jQuery, I am trying to make AJAX form validation work in CodeIgniter. If you have the same title don't add The data from the form always reports as  AJAX Form Submission in Codeigniter Submit AJAX form in Codeigniter with server side form validation and data insert into database.

CodeIgniter AJAX JSON validator with form validation, This is my Ajax form submission code. Code: [removed] $(document).ready(​function() { $('#submit  In this tutorial, i will show you how submit form using jquery ajax without page refresh in codeigniter 3. i will write simple example of submit form using jquery ajax in codeigniter 3 website. When you click on submit button then it will automatically call submit event of jquery.

Codeigniter form validation not working while using Ajax form , Codeigniter Tutorial - you will learn how to submit a form using jQuery ajax with validate a form data in client side without refresh/reload the  CodeIgniter lets you set as many validation rules as you need for agiven field, cascading them in order, and it even lets you prep andpre-process the field data at the same time. To set validation rules youwill use the set_rules()method: $this->form_validation->set_rules();