【问题标题】:Is updating codeigniter db-session data clientside via ajax possible?是否可以通过 ajax 更新 codeigniter db-session 数据客户端?
【发布时间】:2013-07-15 13:30:14
【问题描述】:

我在数据库中使用带有加密会话的 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'); ?>">

"&lt;?php echo $this-&gt;session-&gt;userdata('first_name'); ?&gt;" 在任何页面刷新加载旧数据之前第二次打开模式。

【问题讨论】:

  • 只需使用新值更新您的session。使用$this-&gt;session-&gt;set_userdata('name', value)。这将更新您当前的会话,并且您的模型下一次也将获得更新的会话。
  • 当我在 ajax 中调用服务器上的方法时,我会像你一样更新所有会话数据,但它不会在客户端上更新它
  • 给我看一些你在哪里做的代码。你的model?
  • 更新后如何获取新的会话数据?你从你的控制器返回什么给 JS?
  • 我返回一个 html 字符串,它会创建一个 twitter 引导警报 (cssdeck.com/labs/twitter-bootstrap-alerts-and-notifications),它会替换表单数据,然后表单会在 2 秒后关闭

标签: ajax codeigniter session


【解决方案1】:

当然,您只需要调用一个更新/设置新会话数据的 ajax url:

   HTML+JS 
       ---> Ajax call
          ----> $this->session->set_userdata('key','new-value'); 
            ----> session and db updated.

完成。

还要注意并更改所有这些:

$screenname = $this->security->xss_clean($this->input->post('screenname'));

到这里:

$screenname = $this->input->post('screenname',true);

结果完全一样

【讨论】:

  • profile_model 函数会这样做,但它没有在客户端更新,有什么原因吗?
猜你喜欢
  • 1970-01-01
  • 2016-09-04
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 2010-09-18
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多