代码标识符中的Ajax格式验证

前端之家收集整理的这篇文章主要介绍了代码标识符中的Ajax格式验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
恶魔家伙,

我最近一直在使用ajax,而且我在使用codeigniter表单验证库时遇到问题.我使用工具在函数http://formtorch.geekhut.org/生成的示例.
现在,当我使用带有哑数据的json_encode()函数时,ajax完美地返回数据,但在示例中验证使用验证库而不是form_validation库,这似乎是较旧的版本.

为此,在该示例中,验证不适用于ajax,特别是$this-> form_validation-> run()函数使得ajax不返回结果,即使我在create_course()的开头使用json_encode()回显伪数据) .

所以用ajax进行验证有什么问题,并向我解释由控制器收到的ajax发送的数据.

所以这是我的代码

  1. function create_course()
  2. {
  3. $this->form_validation->set_rules('course_code','course_code','trim|xss_clean|required');
  4. $this->form_validation->set_rules('name','name','xss_clean|required');
  5. // .. etc
  6. if ($this->form_validation->run()) {
  7. // validation ok
  8. $data['course_code'] = $this->form_validation->set_value('course_code');
  9. $data['name'] = $this->form_validation->set_value('name');
  10. // ... etc
  11. if ($this->models_facade->create_course($user_id,$data)) { // success
  12. $data = array( 'profile_change' => $this->lang->line('profile_change'));
  13. } else { // fail
  14. $data = array( 'profile_change_error' => $this->lang->line('profile_change_error'));
  15. }
  16. }
  17. else
  18. {
  19. $data = array(
  20. 'course_code' => $this->form_validation->course_code_error,'name' => $this->form_validation->name_error
  21. );
  22. }
  23. echo json_encode($data);
  24. }

这是Jquery Ajax功能

  1. $(function(){
  2. $("#submit").click(function(){
  3. var course_code = $("#course_code").val();
  4. var name = $("#name").val();
  5. // etc
  6. $.post("<?PHP echo base_url() ?>home/create_course",course_code:course_code,name:name},function(data){
  7. function(data){
  8. alert(data.data);
  9. $("#course_code_error").html(data.course_code);
  10. $("#name_error").html(data.name);
  11. },'json');
  12. });
  13. return false;

});

你使用什么版本的Codeigniter?你有没有记得在你的构造中加载验证库?
  1. $this->load->library('form_validation');

猜你在找的Ajax相关文章