2016-11-01 66 views
0

我正在CodeIgniter的網站上工作。我想檢查日期是否已經存在於數據庫中。我已經編寫所需的功能,但submiting形式時,我得到一個錯誤:檢查日期是否存在jQuery驗證器,ajax和CodeIgneter

POST 500 (Internal Server Error) 

這裏是我的模型:

private function tanggal($date) 
 
\t { 
 
\t \t $this->db->where('start_date', $date); 
 
\t \t $query = $this->db->get('t_trx_activity'); 
 
\t \t if($query->num_rows() > 0){ return TRUE; } else { return FALSE; } 
 
\t }

我的控制器:

function validate_date() 
 
\t { 
 
\t if (array_key_exists('start',$_POST)) { 
 
\t if ($this->activity->tanggal($this->input->post('start')) == TRUE) { 
 
\t \t echo false;; 
 
\t \t } else { 
 
\t \t echo true; 
 
\t \t } 
 
\t \t } 
 
\t }

我的觀點:

<form class="form-horizontal" id="form-date" role="form" method="post" action="<?php echo base_url(); ?>activity/add"> 
 
<div class="form-group"> 
 
<label class="col-sm-3 control-label">Start Date:<span 
 
class="required">*</span> </label> 
 
<div class="col-sm-7"> 
 
<div class="input-group date date-picker" data-date-format="yyyy-mm-dd"> 
 
<input type="text" class="form-control" readonly id="date" name="start" required> 
 
<span class="input-group-addon"> 
 
    <span class="glyphicon glyphicon-calendar"></span> 
 
</span></div> 
 
</div> 
 
</div> 
 
<div class="form-group"> 
 
    <label class="col-sm-3 control-label">End Date:<span 
 
    class="required">*</span> </label> 
 
<div class="col-sm-7"> 
 
<div class="input-group date date-picker" data-date-format="yyyy-mm-dd"> 
 
    <input type="text" class="form-control" readonly name="end" required> 
 
<span class="input-group-addon"> 
 
<span class="glyphicon glyphicon-calendar"></span> 
 
</span></div> 
 
</div> 
 
</div> 
 
<button type="submit" class="btn green"> 
 
<i class="fa fa-pencil"></i> Create</button>

jQuery驗證:

var FormValidation = function() { 
 

 
     var handleValidation = function() { 
 

 
       var form = $('#form-date'); 
 
       var error = $('.alert-danger', form); 
 
       var success = $('.alert-success', form); 
 

 
       form.validate({ 
 
        errorElement: 'span', //default input error message container 
 
        errorClass: 'help-block help-block-error', // default input error message class 
 
        focusInvalid: false, // do not focus the last invalid input 
 
        ignore: "", // validate all fields including form hidden input 
 
        rules: { 
 
         
 
         start: { 
 
          required: true, 
 
          date: true, 
 
          remote: { 
 
            url: "<?php echo site_url('activity/validate_date')?>", 
 
            type: "post", 
 
            data: { 
 
               date: function(){ return $("#date").val(); } 
 

 
              } 
 
             } 
 

 
         }, 
 
         end: { 
 
          required: true 
 
         }, 
 
         
 
        }, 
 

 
        messages: { 
 
          start: 
 
          { 
 
            remote: 'Date already in use.' 
 
          } 
 
         },

這個豪任何想法這可以解決嗎?

+1

maby'echo false ;;'是問題,打開路線在一個新的頁面,看看是否它有任何錯誤 – madalinivascu

+0

有任何PHP錯誤日誌? –

+1

您的模型函數'tanggal'必須是'public'而不是'private' –

回答

0

您發送POST數據作爲Ajax調用的名稱date,但你的名字start接收數據, 試圖改變自己的控制器功能於以下,

function validate_date() 
{ 
    if (array_key_exists('date',$_POST)) { 
     if ($this->activity->tanggal($this->input->post('date')) == TRUE) { 
      echo false;; 
     } else { 
      echo true; 
     } 
    } 
} 

或如果u不想更改控制器功能,你可以改變js代碼,改變你的jQuery驗證器中遠程函數部分的數據部分如下,

data: { 
    start: function(){ 
     return $("#date").val(); 
    } 

} 
+0

如果我的數據名稱爲'start',我該怎麼辦? – ione

相關問題