简体   繁体   中英

JQuery validation data exist in table with space

I want if a data have already existed in record it will be disabled my save button, so far I manage to do that, but if the data contain space it didn't work, I believe it is because sending data through JQuery will change the space into %20 how to overcome this? Below is my code

Here is my JQuery

<!-- Unique Data Validation -->
<script type="text/javascript">
$(document).ready(function(){
  $('#kProject').bind('keyup change',function(){
    var check1=0;
    var name = $(this).val();
    $.ajax({
      url:'project/cekData/master_kategori_project/kategori_project/'+name,
      data:{send:true},
      success:function(data){
        if(data==1){
          $('#report1').text('');
          $('.btn_save').prop('disabled',false);
          check1=1;
        }else{
          $('#report1').text('Project Exist');
          $('.btn_save').prop('disabled',true);
          check1=0;
        }
      }
    })
  })
})
</script>
<!-- End Unique Data Validation -->

and this is my controller

public function cekData($table, $field, $data){
    $match = $this->project_m->read($table, array($field=>$data), null, null);
    if($match->num_rows() > 0){
        $report = 2;// exist
    }else{
        $report = 1;//not exist
    }
    echo $report;
}

and this is my model

public function read($table, $cond, $ordField, $ordType){
    if($cond!=null){
        $this->db->where($cond);
    }
    if($ordField!=null){
        $this->db->order_by($ordField, $ordType);
    }
    $query = $this->db->get($table);
    return $query;
}

example my table looks like below, there is no %20 in my table so when jQuery send the data the data would have something like Jalan%20Tol of course Jalan%20Tol doesn't exist in my table, it only exist Jalan Tol

在此处输入图片说明

PHP has a built-in function for things like that try it that way

public function cekData($table, $field, $data){
    $data = urldecode($data);
    $match = $this->project_m->read($table, array($field=>$data), null, null);
    if($match->num_rows() > 0){
        $report = 2;// exist
    }else{
        $report = 1;//not exist
    }
    echo $report;
}

I believe it is not about the jQuery but the controller you are using. In PHP, I used many times %20 and data.serialize() function which puts 20% as well instead of spaces but never got a problem. Your controller doesn't recognize 20% as space . So you need to change 20% with a value which your controller will recognize as space .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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