2012-02-14 56 views

回答

1

阿賈克斯調用 - > ci控制器 - >控制器驗證 - >控制器調用模型 - >模型句柄db - > ci返回true/false返回到ajax - > ajax句柄。

AJAX調用:

$.ajax({ 
    url: '/index.php/ratings/process', 
    type: 'POST', 
    dataType: 'json', 
    error: function(data){ 
     console.log('your errors are: '+data.errors); 
    }, 
    success: function(data){ 
     console.log('added rating/comments to db!'); 
    } 
}); 

笨控制器稱爲評級:這不會有任何形式的驗證,你應該這樣做。在調用之前或使用條件語句進行驗證。

public function process(){ 
    $field1 = $this->input->post('field1', TRUE); 
    $params = array('field1' => $field1); 
    if (!$this->comments->addComments($params)){ 
     return $this->output->set_status_header(500, 'error submitting to db'); 
    }else{ 
     return $this->output->set_status_header(200, 'success!'); 
    } 
} 

CodeIgniter的型號叫做評論

public function addComments($params = FALSE){ 
    // if params is not equal to false, do work. 
    if (!$params == FALSE){ 
    //insert the params into the database comments 
    $this->db->insert('comments', $params); 
    // if affected rows is greater than 0, return the last inserted ID. 
    if ($this->db->affected_rows() > 0){ 
     return $this->db->insert_id(); 
    }else{ 
     // otherwise return false, fail 
     return false; 
    }else{ 
    // params were false so return false 
    return false; 
    } 
} 

不完整的代碼,但你的想法。

+1

我不使用Skype。你可以在這裏進行你的問題。 – gorelative 2012-02-14 13:35:30

+0

以及..問題是,我不知道任何AJAX ...:/ – 2012-02-14 14:10:33

+0

如果我的答案幫助你,它會很高興得到接受。 – gorelative 2012-02-14 14:21:11