1
通常,AJAX用於通過與後端異步通信(在我的情況下爲CodeIgniter)來動態修改網頁,以避免頁面重新加載。使用會話數據反映重新加載時對網頁的更改是否是一個好主意?
但是,我正在考慮使用會話數據爲做相同的事情,其中頁面重新加載不是問題。
這裏是什麼,我試圖做一個代碼片段:
if($this->Member_model->checkPassword($data)){ //checks whether old password is correct
$this->Member_model->setPassword($new_pass); //sets the new password
$this->session->set_userdata('pass_meta', 'success'); //stores success in session data
}
else {
$this->session->set_userdata('pass_meta', 'fail'); //stores fail in session data
}
redirect('profile','refresh'); //refreshes the page
雖然建立個人資料頁面,這是我在做什麼:
if($this->session->userdata('pass_meta')){ //checks whether the field pass_meta is set in the session
$data['pass_meta'] = $this->session->userdata('pass_meta'); //reads the field into $data (which will be passed to the page)
$this->session->unset_userdata('pass_meta'); //unsets the field in the session
}
else {
$data['pass_meta'] = "";
}
所以成功/失敗消息被傳遞給觸發適當消息的View。
現在,這種方法對我來說是完美的。我的問題是:這是一個好主意嗎?這種設計方法的優點/缺點是什麼?
可以使用Flash消息做同樣的事情嗎?我猜想,一旦我從閃存消息中讀取數據,就不會像在這種情況下那樣解除會話數據。 –
@BlazeSahlzen,是的,這是他們共同的用途之一。從標籤我猜你正在使用CodeIgniter,它已經實現了Flash消息(https://ellislab.com/codeigniter/user-guide/libraries/sessions.html)。 – Looted
好吧,我會試試看。謝謝! –