繁体   English   中英

通过Ajax在数据库中检查数据是否已存在于Codeigniter中

[英]Data check in database if already exist in codeigniter through ajax

我想通过Codeigniter中的Ajax检查数据库中的数据,如何通过Ajax获取已经存在的检查名称

控制者

public function insert(){
    $user = array('Name' => $this->input->post('name'), 
                  'Cnic' => $this->input->post('cnic'),
                  'Phone' => $this->input->post('phone'),
                  'Address' => $this->input->post('address'),
        );
    $this->load->model('suppliermodel');
    if($this->suppliermodel->checkData($user['Name'])){
        if ($this->session->userdata('user_id'))
          $detail = 'Already exist';
      $this->load->view('admin/includes/header');
      $this->load->view('admin/includes/sidemenu');
      $this->load->view('admin/add_supplier',['exist'=>$detail]);
      $this->load->view('admin/includes/footer');
      $this->load->view('admin/includes/add_supplier_footer');

    }
    else{
     $this->suppliermodel->add($user);  
    }

}

模型

public function checkData()
{
  $name = $this->input->post('name');
  $this->db->select('*');
  $this->db->where('Name', $name);
  $this->db->from('suppliers');
  $query = $this->db->get();
  if($query->num_rows() >0){
    return $query->result();
  }
  else{
    return $query->result();
    return false;
  }
}

什么是ajax代码和控制器功能

这个怎么样?

控制者

public function insert(){

    // set a rule that make the Name field is unique by 'set_rules'
    $this->form_validation->set_rules('Name', 'Name Field', 'required|is_unique[suppliers.name]');
    //$this->form_validation->set_rules('[Other Field]', '[Field Name to Display]', '[Restriction]');

    // if the field cannot pass the rule
    if ($this->form_validation->run() === FALSE) {

        $errors = array();

        foreach ($this->input->post() as $key => $value) {
            // Add the error message for this field
            $errors[$key] = strip_tags(form_error($key));
        }
        // Clear the empty fields (correct)
        $response['errors'] = array_filter($errors); 
        $response['status'] = false;

    }
    else {
        // otherwise, call the model
        $result = $this->suppliermodel->add($user);  

        if ( $result ) {
            $response['status'] = true;
        }

    }

    echo json_encode($response);
}

的JavaScript

$.ajax({
    url: '//localhost/insert',
    data: {
        Name: $('input[name=Name]').val(),
        Cnic: $('input[name=Cnic]').val(),
        Phone: $('input[name=Phone]').val(),
        Address: $('input[name=Address]').val()
    },
    dataType: 'JSON',
    type: 'POST',
    error: function(xhr) {
        alert('request failed!');
    },
    success: function(response) {

        var response = $.parseJSON(JSON.stringify(response));

        if (response.status != true) {

            $.each(response.errors, function(field, i) {
                alert( field+ ' errors with ' + i)
            });
        }
        else {

            alert('success!');
        }
    }
});

使用is_unique[table.fieldToCompare]使该字段始终唯一。

希望能有所帮助。

set_rules限制,请参阅Codeigniter用户指南表单验证

如果失败,则控制器将返回一组JSON,其中包含字段和错误消息。 然后,您可以在$.ajax success

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM