javascript - 如何使用 PHP CodeIgniter、JQuery、AJAX 检查数据库中是否已存在 ID 验证

标签 javascript php jquery codeigniter

我正在尝试使用 ajax 和 codeigniter 检查 id 的有效性。当id被占用时,状态会出现“NPP tidak ada di database”。

我的 Controller :

 public function ajax_add()
    {
        $this->_validate();
         $data = array(
        'id' => $this->input->post('id'),
        'username' => $this->input->post('username'),
        'password' => $this->input->post('password'),
        'level' => $this->input->post('level'),
    );
        $insert = $this->user_model->save($data);
        echo json_encode(array("status" => TRUE));
    }

   private function _validate()
   {
$data = array();
$data['error_string'] = array();
$data['inputerror'] = array();
$data['status'] = TRUE;
$id  = $this->input->post('id');
$result     = $this->user->checknpp( $id );  #send the post variable to  the model
//value got from the get metho
$id= $id;

 if( $result == '0' )
 {
    $data['inputerror'][] = 'id';
    $data['error_string'][] = 'NPP tidak ada di database';
    $data['status'] = FALSE;
  }    
 if($data['status'] === FALSE)
  {
    echo json_encode($data);
    exit();
}
}

我的模态:

public function checknpp($id)
{  
   $this->db->select('id');
   $this->db->where('id', $id);
   $this->db->from('id', 1);
   $query = $this->db->get();

   if( $query->num_rows() > 0 ){     return 0;    }
   else{    return 1;   }               
} 

我的js:

function save()
 {
  $('#btnSave').text('saving...'); //change button text
  $('#btnSave').attr('disabled',true); //set button disable 
  var url;

if(save_method == 'add') {
    url = "<?php echo site_url('user/ajax_add')?>";
} else {
    url = "<?php echo site_url('user/ajax_update')?>";
}

// ajax adding data to database
$.ajax({
    url : url,
    type: "POST",
    data: $('#form').serialize(),
    dataType: "JSON",
    success: function(data)
    {

        if(data.status) //if success close modal and reload ajax table
        {
            $('#modal_form').modal('hide');
            reload_table();
        }
        else
        {
            for (var i = 0; i < data.inputerror.length; i++) 
            {
                  $('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
                  $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
            }
        }
        $('#btnSave').text('save'); //change button text
        $('#btnSave').attr('disabled',false); //set button enable 


    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Error adding / update data');
        $('#btnSave').text('save'); //change button text
        $('#btnSave').attr('disabled',false); //set button enable 

    }
  });
}

我的观点:

<input name="id" id="masukpun" placeholder="NPP" class="form-control" type="text" />

但是,当我运行它时,它总是说更新/添加数据时出错。

最佳答案

试试这个,

public function ajax_add() {
    $data = array();
    $data['error_string'] = array();
    $data['inputerror'] = array();
    $data['status'] = TRUE;
    $id = $this->input->post('id');
    $result = $this->user->checknpp($id);  #send the post variable to  the model
    //value got from the get metho
    $id = $id;

    if ($result == '0') {
        $data['inputerror'][] = 'id';
        $data['error_string'][] = 'NPP tidak ada di database';
        $data['status'] = FALSE;
    } else {
        $data = array(
            'id' => $this->input->post('id'),
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),
            'level' => $this->input->post('level'),
        );
        $insert = $this->user_model->save($data);
        $data = array("status" => TRUE);
    }
    echo json_encode($data);
}

关于javascript - 如何使用 PHP CodeIgniter、JQuery、AJAX 检查数据库中是否已存在 ID 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34169624/

相关文章:

javascript - 如果选择网站更新框,请通知我?

php - 11 月 30 日 -0001 按时间戳返回

php - 如何在 HTTP 请求和 cli 类对象之间进行对话

jquery - 在 JQuery 中滑动水平 div

javascript - 将两个数组传递给函数,使用数据来启动按钮按下。 JavaScript

javascript - 如何清除在组件生命周期中创建的 setTimeout?

javascript - 防止浏览器捕捉到后退按钮上的上一个滚动位置

C# - MySQL 数据库,加密时检查密码是否正确?

javascript - 如果我的网站链接到外部样式表,我应该担心吗

javascript - 单击不适用于新元素