私はデータベース内の暗号化されたセッションで codeigniter を使用しており、フォーム内のユーザーの詳細を更新するために Twitter ブートストラップ モーダルを使用しています。
フォームでjquery検証を使用し、submitHandlerでajaxを介してデータを投稿し、モーダルを閉じます。
submitHandler: function (form) {
document.getElementById("edit-profile-submit-button").disabled = true;
$('.modal-ajax-loader').show();
$.ajax({
type: $(form).attr('method'), // 'Post'
url: $(form).attr('action'), // 'profile/edit_basic_details'
data: $(form).serialize(),
success: function(data, status){
$(form).html(data);
$('.modal-ajax-loader').hide();
setTimeout(function() { $('#edit-profile-details').modal('hide'); }, 2000);
},
error: function(data, status) {
$(form).html(data);
}
});
return false;
}
そして、これが同じ名前のコントローラーから呼び出されるモデル関数です。
function edit_basic_profile() {
$screenname = $this->security->xss_clean($this->input->post('screenname'));
$firstname = $this->security->xss_clean($this->input->post('firstname'));
$lastname = $this->security->xss_clean($this->input->post('lastname'));
$email = $this->security->xss_clean($this->input->post('email'));
$bio = $this->security->xss_clean($this->input->post('bio'));
$data = array(
'screen_name' => $screenname,
'first_name' => $firstname,
'last_name' => $lastname,
'email' => $email,
'bio' => $bio,
);
try{
// Run the update query
$this->db->where('profile_id', $this->session->userdata('profile_id'));
$this->db->update('profiles', $data);
// Let's check if there are any results
if($this->db->affected_rows() == 1)
{
// Setup the session information for the user
$this->session->set_userdata($data);
return true;
}
// If the previous process did not update rows then return false.
error_log("profile_model, edit_basic_profile(): There were no affected rows");
return false;
} catch(PDOExceprion $e) {
error_log("profile_model, edit_basic_profile(): ".$e);
return false;
}
}
ページで変更された値を submitHandler で更新することもできます。もちろん、サーバー上のセッションはモデルで更新されます。
$("#profile-screenname").html($(screenname).val());
$("#profile-bio").html($(bio).val());
問題は、モーダルを再度開くと、ブラウザの Cookie のセッション データからユーザーの詳細を取得し、最初の更新後にページが更新されていない限り、元のデータを取得することです。
(フォームデータはこのように読み込まれます);
<input type="text" class="input-large" id="firstname" name="firstname" placeholder="First Name" value="<?php echo $this->session->userdata('first_name'); ?>">
"<?php echo $this->session->userdata('first_name'); ?>"
2 回目は、ページの更新で古いデータが読み込まれる前にモーダルを開きます。