codeigniter ajax form_validation(使用jquery)

jah*_*jah 0 php ajax jquery codeigniter

我试图通过codeigniter来验证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)
        });

    })

})
Run Code Online (Sandbox Code Playgroud)

这基本上让我的所有输入字段在keyup上发送它们的值(1秒后).

我的php(只是用户名测试的一个片段):

<?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;
            }
        }

    }

?>
Run Code Online (Sandbox Code Playgroud)

我收到的响应是一个PHP错误:

消息:未定义的变量:form_error

致命错误:函数名称必须是第401行的\ PATH TO CODEIGNITER\application\modules\auth\controllers\auth.php中的字符串

希望有人知道如何解决这个问题,或者以另一种方式做到这一点.先感谢您.

Gio*_*ece 6

在"form_error"之前取出$.

$response = array($validates,$form_error('username'));
Run Code Online (Sandbox Code Playgroud)

$response = array($validates,form_error('username'));
Run Code Online (Sandbox Code Playgroud)