jquery验证远程无法在codeigniter中工作

时间:2017-04-14 04:42:35

标签: php jquery codeigniter

我尝试使用远程规则来检查add_book表中的详细信息(标题)是否已经存在...但是使用以下代码,它没有给出任何结果...... 脚本: -

title: {
        required: true,
        remote: {
        url: "<?php echo base_url('add_books/check_title_exists'); ?>",
        type: "post",
           }
         },

控制器: -

public function check_title_exists() {
        $title = $this->input->post('title');
        $check_title = $this->add_book_model->check_title($title);
        if ($check_title > 0) {
            return json_encode(false);
        } else {
            return json_encode(true);
        }
    }

型号: -

public function check_title($title) {
        $this->db->where('title', $title);
        $query = $this->db->get('add_book');
        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

2 个答案:

答案 0 :(得分:1)

请尝试这对我工作正常: - 在脚本中

title: {


 required: true,
                           remote: {
                               url: "<?php echo base_url('add_books/check_title_exists'); ?>",
                               type: "post",
                               data: {
                                   title: function () {
                                       return $("#title").val();
                                   }
                               }
                           }
                       },

在控制器中: -

function check_title_exists(){

       $count= $this->add_book_model->isTitleExists($this->input->post('title'));
           if ( $count == TRUE ) {
               echo json_encode(FALSE);
           } else {
               echo json_encode(TRUE);
           }

   }
 public function isTitleExists($title) {
           $query = $this->db
                   ->select('title')
                   ->where('title', $title)
                   ->get('books');
           if( $query->num_rows() > 0 ){
               return TRUE;                 
           } else { 
               return FALSE;                
           }

   }

答案 1 :(得分:0)

控制器:

function checkremote(){
    $uName['email'] = $this->input->post('email');
    $isUNameCount = $this->User_model->alreadyexits('candidate',$uName);
    if($isUNameCount > 0){
            echo 'false';
    }else{
         echo 'true';
    }
}

型号:

function alreadyexits($table,$var) { 
  $query = $this->db ->select('*') ->where($var) ->get($table) ->num_rows(); return $query;
}

验证:

email: {
  required: true,
  email: true,
  remote: {
    url:"", 
    type: "post",
    data:{
      name:function(){
        return $('#email').val()
      }
    }
  }
}