jquery - My cookie gets reset after clicking button -
i have joomla site , using rsform component create multi-page form. saving input field values in cookie before form submitted in case user decides can't finish filling out form. if user returns have function take cookie , loop through form , populate form fields values stored in cookie. can create , read cookie can't repopulate form. here flow.
- user goes website , website checks if have cookie stored on machine if not gets created.
- the cookie gets populated when user clicks button go next form view @ time take form , create json stringify serialized array , pass cookie stored.
but noticed cookie not populated values until 3 form views deep. example if fill in first 2 textareas , click next button next form view, in console see cookie key pairs values empty. if fill in next textarea , click next button can see value first 2 textareas in console not textarea filled out. hope guys can understand trying say. have been @ weeks , can't figure out appreciated. here link form http://hearwithyourheartusa.com/index.php/share-your-story. maybe easier see trying rather trying understand typed out.
edit here code using set , cookie , code should populate form values of ccokie. have jsfiddle refuses set cookie here link if want give shot http://jsfiddle.net/uqj4x/
function createcookie(name,value,days) { var expires = ""; if (days) { var date = new date(); date.settime(date.gettime()+(days*24*60*60*1000)); var expires = "; expires="+date.togmtstring(); } document.cookie = name+"="+value+expires+"; path=/"; } function readcookie(name) { var nameeq = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charat(0)===' ') c = c.substring(1,c.length); if (c.indexof(nameeq) === 0) return c.substring(nameeq.length,c.length); } return null; } function string2form($form, serializedstr) { var fields = json.parse(serializedstr); (var = 0; < fields.length; i++) { var controlname = fields[i].name; var controlvalue = fields[i].value; $form.find("[name='+ controlname +']").attr('value',controlvalue); //document.getelementsbyname(controlname).value = controlvalue; //console.log('field name='+controlname+' field value='+controlvalue); } console.log('form values - '+json.stringify(jquery('#userform').serializearray())); var cookie = readcookie('storycookie'); console.log('the cookie values '+cookie); } jquery('#userform input:button').click(function() { // jquery.ajax({ // cache: false, // type: 'post', // url: 'http://hearwithyourheartusa.com/templates/jsn_boot_pro/customphp/ajaxcookie.php', // data: json.stringify(jquery('#userform').serializearray()), // contenttype:"application/json; charset=utf-8", // datatype: 'json', // success: function(data){ // alert(data); // } // }); console.log(id); createcookie('storycookie',json.stringify(jquery('#userform').serializearray()),365); var cookie = readcookie('storycookie'); console.log('the cookie '+cookie); });
i tried filling in form it's not setting cookies. couldn't find code saves cookies (it should in rsfp_changepage, right?)
anyhow, did set cookie expiration time in future?