带验证的插入函数上的Codeigniter Ajax

Codeigniter Ajax on insert function with validation

本文关键字:Codeigniter Ajax 插入 验证 函数      更新时间:2023-09-26

我有这个表单,可以在代码点火器上插入数据。它有验证,我只想让它成为ajax。我该如何在上面安装ajax?

我的代码,

控制器

//创建////////

public function create(){
    $this->form_validation->set_rules('PROVINCE','Province Name','trim|required|max_length[30]|callback_if_exist');
    if($this->form_validation->run($this) == FALSE){
        $this->add_view();
    }else{
        if($query = $this->Provinces_Model->insert()){
            redirect('Provinces/index');
        }else{
            $this->add_view();
        }
    }
}

///////////检查重复

public function if_exist(){
    $available = $this->Provinces_Model->check_if_exist(); //change
    if($available){
        return TRUE;
    }else{
        $this->form_validation->set_message('if_exist','{field} already exist!');
        return FALSE;
    }
}

型号

///回调

public function check_if_exist(){
    $sql = "SELECT * FROM $this->table WHERE PROVINCE = ?";
    $data = array('PROVINCE' => $this->input->post('PROVINCE'));
    $query = $this->db->query($sql, $data);
    if($query->num_rows() == 0){
        return TRUE;
    }else{
        return FALSE;
    }
}

/////创建////////

public function insert(){
    $input = array(
            'PROVINCE' => $this->input->post('PROVINCE')
            );
    $insert = $this->db->insert($this->table,$input);
    return $insert;
}

最后是视图

            <div class="box-body">
              <div class="form-group">
                <label for="PROVINCE" class="col-sm-2 control-label col-sm-offset-2">Province Name:</label>
                  <div class="col-sm-5">
                    <input type="text" class="form-control" id="PROVINCE" name="PROVINCE" value = "<?= set_value("PROVINCE"); ?>">
                  </div>
              </div>
              <div class="form-group has-error col-sm-offset-7">
                <label class="control-label" for="error"><?php echo form_error("PROVINCE"); ?></label>
              </div>
            </div>

注意,我有错误报告so和value = "<?= set_value("PROVINCE"); ?>",用于在其值未通过验证时保留我插入的值。。现在我想把它作为一种实践,因为我要做的下一部分是在我提交一个表单后,它将转到另一个表单。

如下编辑代码:

控制器:

public function create(){
    $this->form_validation->set_rules('PROVINCE','Province Name','trim|required|max_length[30]|callback_if_exist');
    if($this->form_validation->run() == FALSE){
        echo 'ERR';
    }else{
        if($query = $this->Provinces_Model->insert()){
            redirect('Provinces/index');
        }else{
            $this->add_view();
        }
    }
}

视图的Javascript部分:

    <script type="text/javascript" >
    $('#send').click(function(){ // you should add an id called "send" to your submit button
   var province = $('#province').val();
        $.ajax({ 
            type:'POST',
            url:'yourposturl', 
            data: province,
            success:function(response){
              if (response = 'ERR') {
                $("#response").html('Some error text');
                 }
                else { 
                $("#response").html('Some success text');   
                }
            }
        });
      });
    </script>

如果您想使用此方法,请将<label class="control-label" for="error">部分更改为<label class="control-label" id="response" for="error">