【问题标题】:When form validation fails unable to receive ajax data in codeIgniter当表单验证失败时无法在codeIgniter中接收ajax数据
【发布时间】:2016-05-06 08:42:52
【问题描述】:

这是我在 codeIgniter 中的第一个项目。在这里,我尝试使用 Validator 控制器类的 form_submit() 方法来验证表单...我通过调用 Validator 控制器的 get_cities() 方法和 get_sub_category () 方法,使用 jquery 的 ajax 方法在表单中生成两个下拉字段。值得一提的是,我正在使用与表单视图链接的外部 dropdown.js 文件。这是 dropdown.js 文件

$(document).ready(function () {
$('#city_h').hide();
$('#sub_category_h').hide();   
//function to dynamically populate CITY according to STATE
$('#state').change(function () {
    var state_id = $('#state').val();
    var state_id = state_id.trim();
    if (state_id/* != ""*/) {
        //Need to handle form submission more efficiently
        /**
         * For the time being, i am checking if the user has submitted the form  or not
         * By the following if else block but in future i will try to handle it from CONTROLLER
         */
        if ($(location).attr('href') == "http://localhost/ci_practice/") {
            var post_url = "index.php/validator/get_cities/" + state_id;
        } else {
            var post_url = "get_cities/" + state_id;
        }
        //Need to handle form submission more efficiently
        $.ajax({
            type: "POST",
            url: post_url,
            success: function (cities) //we're calling the response json array 'cities'
            {
                $('#city').empty();
                $('#city_h').show();
                $.each(cities, function (ci_id, ci_name)
                {
                    var opt = $('<option />'); // here we're creating a new select option for each group
                    opt.val(ci_id);
                    opt.text(ci_name);
                    $('#city').append(opt);
                });
            } //end success
        }); //end AJAX
    } else {
        $('#city').empty();
        $('#city_h').hide();
    }//end if
}); //end change
//function to dynamically populate SUBCATEGORY according to CATEGORY
$('#category').change(function () {
    var category_id = $('#category').val();
    var category_id = category_id.trim();
    if (category_id/* != ""*/) {
        //Need to handle form submission more efficiently
        /**
         * For the time being, i am checking if the user has submitted the form  or not
         * By the following if else block but in future i will try to handle it from CONTROLLER
         */
        if ($(location).attr('href') == "http://localhost/ci_practice/") {
            var post_url = "index.php/validator/get_sub_category/" + category_id;
        } else {
            var post_url = "get_sub_category/" + category_id;
        }
        //Need to handle form submission more efficiently
        $.ajax({
            type: "POST",
            url: post_url,
            success: function (subcategories) //we're calling the response json array 'cities'
            {
                $('#sub_category').empty();
                $('#sub_category_h').show();
                $.each(subcategories, function (sc_id, sc_name)
                {
                    var opt = $('<option />'); // here we're creating a new select option for each group
                    opt.val(sc_id);
                    opt.text(sc_name);
                    $('#sub_category').append(opt);
                });
            } //end success
        }); //end AJAX
    } else {
        $('#sub_category').empty();
        $('#sub_category_h').hide();
    }//end if
}); //end change
});

这里是验证器控制器........

              public function form_submit(){
                  //all form validation code goes here
              }

              public function get_cities($state){
      //this method is called from dropdown.js file to populate subcategory dropdown 
      //but when url changes... this function become unaccessible by dropdown.js     
                            header('Content-Type: application/x-json; charset=utf-8');
                            echo(json_encode($this->form_model->get_cities_by_state($state)));
                }

              public function get_sub_category($category){
      //this method is called from dropdown.js file to populate subcategory dropdown 
      //but when url changes... this function become unaccessible by dropdown.js                          
                            header('Content-Type: application/x-json; charset=utf-8');
                            echo(json_encode($this->form_model->get_subcategory_by_category($category)));
                }

}`

因此,当我提交表单验证失败时,它会转到 url (http://localhost/ci_practice/index.php/validator/form_submit),然后 jquery 的 ajax 方法无法访问 Validator 控制器的 get_cities() 方法和 get_sub_category() 方法,因此我无法重新填充我的下拉列表列表。如何处理这个问题?目前我正在使用这个解决方案

/**
     * For the time being, i am checking if the user has submitted the form  or not
     * By the following if else block but this is not a portable solution
     */
    if($(location).attr('href')=="http://localhost/ci_practice/"){  
        var post_url = "index.php/validator/get_cities/"+state_id;
    } else{
        var post_url ="get_cities/"+state_id;
    }

在我的外部 javascript 文件中,但我认为这不是一个可移植的解决方案。

【问题讨论】:

标签: php jquery ajax codeigniter


【解决方案1】:

问题解决了.........只需要在外部dropdown.js 文件(如 localhost/ci_practice/index.php/validator/get_sub_category)中使用 var post_url 中的完整 URL,而不是像 (index .php/validator/get_cities/)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 2010-12-19
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多