meomike2000 Posted June 12, 2009 Share Posted June 12, 2009 as i am still very new with working with ajax and js.... i need some help getting the value of a cookie...... this is all just for learning by the way.... the cookie is set when a php script validates a user, if ($loginId == 'mike' && $password == 'mike11') { //setcookie('userID', 12345); setcookie("user", $loginId, mktime()+1209600); $respType = 'success'; $respMsg = 'userhome.html'; }else{ i know this part works! here is the ajax part.... look to this.init = function() @@@ self.user = document.cookie.value; ... how should this be..... var user = new function() { this.ajax = null; this.user = ''; this.init = function() { var self = user; self.user = document.cookie.value; self.ajax = new Ajax(); self.getinfo(); }; this.getinfo = function() { var self = user; self.ajax.doGet('/ajaxtest/getuser.php?user=' + self.user, self.setresult); }; this.setresult = function(str) { var self = user; var respArr = str.split(','); var name = respArr[0]; var number = respArr[1]; var userSpan = document.getElementById('userSpan'); var userDiv = document.getElementById('userDiv'); if (userSpan.firstChild) { userSpan.removeChild(userSpan.firstChild); } if (userDiv.firstChild) { userDiv.removeChild(userDiv.firstChild); } userSpan.appendChild(document.createTextNode(number)); userDiv.appendChild(document.createTextNode(name)); }; this.clearCookie = function(name) { var expireDate = new Date(0); document.cookie = name + '=; expires=' + expireDate.toGMTString() + '; path=/'; }; this.cleanup = function() { var self = user; self.ajax = null; self.user = null; self.clearCookie(user); }; }; window.onload = user.init; window.onunload = user.cleanup; thanks mike..... Link to comment https://forums.phpfreaks.com/topic/161967-solved-help-getting-value-from-a-cookie/ Share on other sites More sharing options...
meomike2000 Posted June 12, 2009 Author Share Posted June 12, 2009 ok i got it worked out... this is the change that i made: change in this.init self.user = self.getCookie('user'); then added the getcookie function....... and now all is working well....... this.init = function() { var self = user; self.user = self.getcookie('user'); self.ajax = new Ajax(); self.getinfo(); }; this.getcookie = function(c_name) { var self = user; if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "="); if (c_start!=-1) { c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length; return unescape(document.cookie.substring(c_start,c_end)); } } return ""; } Link to comment https://forums.phpfreaks.com/topic/161967-solved-help-getting-value-from-a-cookie/#findComment-854672 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.