0

I am trying to validate some fields witj ajax through codeigniter, but can't quite figure out how to it it "right".

My ajax:

var timeout = null;

$(document).ready(function(){

    $('.new-user-box input').each(function(){

        var key = $(this).attr('name');

        $(this).on("keyup", function() {
            var value = $(this).val();

            if(value=="") {
                return false;
            }

            var json = {};
            json[key] = value;
            json['ajax'] = '1';

            if (timeout) {
                clearTimeout(timeout);
            }
            timeout = setTimeout( function() {
                $.ajax({
                url: 'auth/ajax_validate',
                type: 'post',
                data: json,
                success: function(data) {
                    console.log(data);
                }
                })
            }, 1000)
        });

    })

})

This basically lets all my input fields send their value on keyup (after 1 second).

My php (just a snippet from the username test):

<?php

    function ajax_validate()
    {

        // Test if the method is called by ajax and validate the input field
        if($this->input->post('ajax'))
        {
            if($this->input->post('username'))
            {
                if($this->form_validation->set_rules('username', 'Brugernavn', 'required|trim|min_length[1]|max_length[20]|is_unique[users.username]|xss_clean') && !$this->form_validation->run())
                {
                    $validates = 0;
                }
                else {
                    $validates = 1;
                    $error = "";
                }
                $response = array($validates,$form_error('username'));
                echo json_encode($response);
                exit;
            }
        }

    }

?>

The response i receive is a php error:

Message: Undefined variable: form_error

Fatal error: Function name must be a string in \PATH TO CODEIGNITER\application\modules\auth\controllers\auth.php on line 401

Hope someone has a clue how to fix this, or do it another way. Thankyou in advance.

0

2 Answers 2

6

Take out the $ before "form_error".

$response = array($validates,$form_error('username'));

to

$response = array($validates,form_error('username'));
Sign up to request clarification or add additional context in comments.

3 Comments

@SpYk3HH Yup I like it :D
Wow, can't believe how unbelievably stupid i feel now, that i didn't see that. Thankyou!
@Jens Ahlsten Herlevsen: are you showing the errors individualy, on each field? how do you output them in jquery?? ( i guess you dont use console.log(data) )
0

Just found this:

$this->input->is_ajax_request();

from here:

Way to tell if a post came from an ajax call in codeigniter?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.