如何通过codeigniter中的ajax获取验证错误

时间:2013-12-16 14:04:26

标签: php codeigniter

我正在尝试在codeigniter中的提交表单中使用ajax。我有它到ajax调用的地方,没有显示验证错误。我无法弄清楚为什么。请帮忙。

我确实有一些回报,但他们什么也没做。

    if ($this->form_validation->run() == FALSE)
   {
   echo(json_encode("validate"=>FALSE));
   }
   else
   {
  $this->load->model('adduser_model');
  $data['query']=$this->adduser_model->adduser();
   }
}

查看代码:

<script>
    //CHECKS ONE FIELD AT A TIME
    $(function(){
        $(".field").each(function(){
            $(this).keyup(function(){
            var id = $(this).attr("id"); //VALUE OF INPUT ID Ex: <input id="name">
            var v = $(this).val(); //INPUT TEXT VALUE
            var data = id+"="+v; //DATA TO GO TO THE AJAX FILE Ex:(name=wcet)

            $.ajax({
                type: "POST",
                url: "prog/validate", //AJAX FILE
                data: data+"&single=true",
                success: function(e){ //"e" IS THE DATA FROM "validate.php"
                    $("span#"+id).html(e); //ECHOS DATA FROM "validate.php" NEXT TO THE     INPUT IF NEEDED
                }
            });
        });
    });
});
</script>

 <BODY>
<?php $this->load->helper('form');
echo form_open('prog/validate'); ?>
<tr><td align="right">Name: </td><td align="left"><input class="field" name="name"      id="name"> <span id="name"></span><br></td></tr>
<tr><td align="right">email: </td><td align="left"><input class="field" name="email" id="email"> <span id="email"></span><br></td></tr>

1 个答案:

答案 0 :(得分:0)

如果您have some returns表示该功能成功。即使您没有达到预期的响应,$this->form_validation->run()仅在$ajax呼叫根本无法触发时才会为假(404,500错误)。

您还可以尝试通过failure: function () {}捕获错误,例如:

$.ajax({
    type: "POST",
    url: "prog/validate", //AJAX FILE
    data: data+"&single=true",
    success: function(e){ //"e" IS THE DATA FROM "validate.php"
        $("span#"+id).html(e); 
    },
    failure: function(e) {
        // check against error messages
    }
});