繁体   English   中英

Codeigniter Ajax form_validation(与jquery)

[英]codeigniter ajax form_validation (with jquery)

我正在尝试通过codeigniter验证一些与Ajax相关的字段,但无法完全弄清楚它的“正确性”。

我的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)
        });

    })

})

基本上,这使我所有的输入字段都可以在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;
            }
        }

    }

?>

我收到的响应是php错误:

消息:未定义的变量:form_error

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

希望有人知道如何解决此问题,或以其他方式解决。 先感谢您。

在“ form_error”之前取出$。

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

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

刚发现这个:

$this->input->is_ajax_request();

从这里:

如何判断帖子是否来自codeigniter中的ajax调用?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM