CakePHP 2:如何存储多项选择的答案会话

时间:2015-06-27 17:49:38

标签: php session cakephp choice

我想存储用户选择的答案,如下图所示:

enter image description here

当用户在另一个问题ID中更新他们的答案时,我遇到了麻烦。 我在传递用户回答时使用url参数,然后使用ajax请求在服务器端处理它。

以下是我的代码:

//php code in server side
$answer[] = array(
        'questionItemId' => $questionItemId, 
        'choice' => $choice
    );
    $newSession = array();

    if(!$this->Session->check('answerChoice')) {
        $this->Session->write('answerChoice', $answer);
    } else {
        $sessionChoice = $this->Session->read('answerChoice');
        foreach($sessionChoice as $itemChoice) {
            if($itemChoice['questionItemId'] == $questionItemId) {
                if($itemChoice['choice'] != $choice) {
                    $newSession[] = array(
                        'questionItemId' => $questionItemId, 
                        'choice' => $choice
                    );
                }
            }
        }
        $this->Session->write('answerChoice', $newSession);
    }
    debug($this->Session->read('answerChoice'));

//ajax js in client side
$('#choiceA, #choiceB, #choiceC, #choiceD').on('click', function() {
    $.ajax({
        url: BASE_URL + 'ajaxed_handlers/refresh_answer/' + $(this).attr('question-item-id') + '/' + $(this).val(),
        success:function(data){
            $('#theform').html(data); //refresh answer done list
        }
    });
});

调试结果如下图所示:

图片问题1 enter image description here

图片问题2 enter image description here

任何帮助非常感谢。感谢。

1 个答案:

答案 0 :(得分:0)

我已经为这个问题做了。我改变了从会话中存储答案的方式,以便从ajax请求中提交表单。

感谢您的关注。

相关问题