7

为什么我的 ajax 请求确实得到一个FALSE字符串(它应该从控制器获取错误消息)?

这是控制器的片段

    function ajax_verify(){
    $this->form_validation->set_rules('username', '', 'max_length[25]|min_length[4]|trim|required|xss_clean');
    $this->form_validation->set_rules('email', '', 'valid_email|required|trim|xss_clean');  
    
    if($this->form_validation->run()==FALSE){
             $errors = $this->form_validation->error_array();
             echo json_encode($this->form_validation->error_array());
    }else{
            echo "Success!";
       }
    
    }

我扩展了库以获取错误消息(它在 php 验证中工作得非常好)

class MY_Form_validation extends CI_Form_validation{

   
function __construct(){
    parent::__construct();
}

function error_array(){
    if(count($this->_error_array)===0){
        return FALSE;
    }else{
        return $this->_error_array;
    }
}

function get_tae(){
    return "TAE!";
 }
}

最后是视图和 jquery ajax 代码(返回 false 而不是错误)。

     <!DOCTYPE HTML>
     <html>
     <head>
          <meta charset="utf-8">
          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
         <title>Title</title>
     <body>

    <?php //echo validation_errors('<div class="error">', '</div>');?>
    <?php echo form_open('ajax/ajax_verify', array('id'=>'my_form'));?>
    <?php echo form_label('Username'); ?>
    <?php echo form_input('username', '', array('id' => 'username'));?>
    <?php echo form_label('Email Address'); ?>
    <?php echo form_input('email', '', array('id' => 'email')); ?>
    <?php echo form_submit('submit', 'Submit'); ?>
    <?php echo form_close();?>

    <script type="text/javascript">
     $(function(){
          $('#my_form').submit(function(e){
                $.get('ajax_verify', function(data){
                    $('#contents').append(data);
                }, 'json') ;                   
                e.preventDefault();
            })
            
    
      });
           </script>
4

2 回答 2

8

尝试这个。它会正常工作。

在控制器中添加这两个方法

 public function CreateStudents() {

        $this->load->helper('form');

        $data['title'] = "Create Students Page";
        $data['success'] = "";
        $this->load->view('templates/header', $data);
        $this->load->view('createstudents', $data);
        $this->load->view('templates/footer', $data);

    }

    public function CreateStudentsAjax() {

        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('', '');

        $this->form_validation->set_rules('roll', 'Roll Number', 'required');
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('phone', 'Phone', 'required');

        if ($this->form_validation->run()) {

            $this->welcome_model->InsertStudents();
            echo json_encode("Oks");
        } else {

            $data = array(
                'roll' => form_error('roll'),
                'name' => form_error('name'),
                'phone' => form_error('phone')
            );

            echo json_encode($data);
        }
    }

在视图中添加表单和一个名为“消息”的DIV

<div id="message">


        </div> 

        <?php echo form_open('welcome/CreateStudentsAjax'); ?>

        <label for="roll">Student Roll Number</label>
        <input type="text" id="txtRoll" value="" name="roll"/>

        <label for="Name">Students Name</label>
        <input type="text" id="txtName" value="" name="name"/>

        <label for="Phone">Phone Number</label>
        <input type="text" id="txtPhone" value="" name="phone"/>

        <input type="submit" name="submit" value="Insert New Students"  />

        <?php echo '</form>'; ?>

现在脚本包含

<script type="text/javascript">

            $(document).ready(function(){

                $('form').submit(function(){
                    //alert('ok');      
                    $.ajax({
                        url:this.action,
                        type:this.method,
                        data:$(this).serialize(),
                        success:function(data){
                            var obj = $.parseJSON(data);

                            if(obj['roll']!=null)
                            {                               
                                $('#message').text("");
                                $('#message').html(obj['roll']);
                                $('#message').append(obj['name']);
                                $('#message').append(obj['phone']);
                            }
                            else
                            {                               
                                $('#message').text("");
                                $('#message').html(obj); 
                            }

                        },
                        erro:function(){
                            alert("Please Try Again");
                        }                        
                    });
                    return false;
                });                        
            });

        </script>
于 2012-09-24T09:43:21.210 回答
6

它的:

echo json_encode(validation_errors());

或 - 单独

$arr = array(
    'field_name_one' => form_error('field_name_one'),
    'field_name_two' => form_error('field_name_two')
);

echo json_encode($arr);
于 2012-04-22T01:02:53.973 回答