JVG
JVG

Reputation: 21150

How can I store data from a jquery survey?

I'm working on a jquery survey (or wizard, I'm not sure of the distinction) that asks the user several questions in succession, and then takes the answers input by the user and uses them to output certain results.

For example, one of my 'questions', where the user will be asked to select his body type:

http://www.jsfiddle.net/T6Jqx/15/

At the moment I have the "rel" attribute of each selection being written to a debug div, can I somehow store this for longer term use?

I want to be able to recall this later on (so at the end of the survey, if var questionOne = "2" then output "xyzcode"; if questionOne = 3 then output "abccode" etc). Should I be using a cookie or something? All of this is new to me..

Upvotes: 0

Views: 256

Answers (1)

RAMe0
RAMe0

Reputation: 1257

If you are going to make different page for each question you should store the results in cookies. For example you can use jQuery.cookie plugin and JSON

for example:

//Get cookies when page loaded
var useranswers=$.cookie('survery');
useranswers= JSON.parse (useranswers);

// do something with previous answers

$('area').hover(
    function(e){
        $('#'+ this.alt).addClass('hover');
    },
    function(e){
        $('#'+ this.alt).removeClass('hover');
    })
    .click(function(e){
        $('img.selected-region').removeClass('selected-region');
        $('#'+ this.alt).addClass('selected-region');

        // jQuery.extend merges contents of two or 
        // more objects together into the first object
        $.extend(useranswers,{ 
                    answer1 : $("img.selected-region").attr('rel') 
        });

        $.cookie(
            'survery', 
            JSON.stringify(useranswers),
            { expires: 7, path: '/', domain: '<your_domain_name>' }
        );
    });

Upvotes: 1

Related Questions